Re: Lengthy numbers

Liste des GroupesRevenir à cl python 
Sujet : Re: Lengthy numbers
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 23. Dec 2024, 09:02:08
Autres entêtes
Organisation : Stefan Ram
Message-ID : <extend-20241223090009@ram.dialup.fu-berlin.de>
References : 1 2
Oscar Benjamin <oscar.j.benjamin@gmail.com> wrote or quoted:
x = [0]; x.extend(iter(x))

  Yo, that's a gnarly piece of Python code!

  This code whips up an infinite list, but in a pretty slick way.

  Here's the lowdown:

    - First off, we're cooking up a list x with just one ingredient: 0

    - Then we're using extend() to pile more stuff onto x

    - The mind-bender is what we're extending with: iter(x)

  iter(x) spins up an iterator from the list x. When we extend x
  with this iterator, it starts tossing the elements of x back into
  itself. But here's the kicker: as x grows, the iterator keeps
  chugging along, creating an endless loop.

  So in theory, this would crank out an infinite list that looks like
  [0, 0, 0, 0, ...]. In the real world, though, if you try to print or
  use this list, your program will probably freeze up faster than the
  405 at rush hour.

  It's a pretty clever (and kind of diabolical) way to create an
  infinite data structure. Just watch out using something like
  this in actual code - it could cause some serious brain freeze!



Date Sujet#  Auteur
23 Dec 24 * Re: Lengthy numbers5Oscar Benjamin
23 Dec 24 +* Re: Lengthy numbers3Stefan Ram
24 Dec 24 i`* Re: Lengthy numbers2Paul Rubin
24 Dec 24 i `- Re: Lengthy numbers1Stefan Ram
24 Dec 24 `- Re: Lengthy numbers1Paul Rubin

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal