| Deutsch English Français Italiano |
|
<extend-20241223090009@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Lengthy numbers
Date: 23 Dec 2024 08:02:08 GMT
Organization: Stefan Ram
Lines: 30
Expires: 1 Jan 2026 11:59:58 GMT
Message-ID: <extend-20241223090009@ram.dialup.fu-berlin.de>
References: <eXx9P.64901$o72.28550@fx01.ams4> <mailman.14.1734910364.2912.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de fIDjdGhIKBrS7ygw9Ijh0gV9PGSbprUWRexjWDkhEnoFM5
Cancel-Lock: sha1:YV22kRBYR4fzBx4UI9Nvs5GmZhs= sha256:uvNAXuMbZDNoVYg0AmPFPeEh3Ra8myKXK0rLwfuv+2M=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 2519
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!