Initializing a list with copies

Steve Holden sholden at holdenweb.com
Tue Apr 30 12:25:13 EDT 2002


"Alex Martelli" <aleax at aleax.it> wrote ...
> Thomas Heller wrote:
>
> > "Alex Martelli" <aleax at aleax.it> wrote ...
> >> Ralf Juengling wrote:
> >>         ...
> >> > BTW: It is not easy to add __iter__ to list's meta-type, is it?
> >>
> >> list's "metatype" (metaclass) is the built-in type named 'type'.
> >> like for all other built-in types and new-style classes.  Adding
> >> __iter__ to type would thus make almost 'everything' iterable.
> >>
> > Correction: adding __iter__ to type would make instances of
> > type iterable (these things are normally called 'classes').
>
> Classes (new-style) and built-in types, as I said.  Maybe I wasn't
> as clear as needed to express once again the distinction between
> levels -- instances of a metaclass are types (classes), so thanks
> for pointing it out.
>
> > Not too useful, it seems.
>
> Particularly given that any class or type (or other callable) that
> is callable without arguments can be used, in the same was as
> Ralg, the OP, wanted to use an "iterable list", by passing it
> in as the first argument to the two-argument form of iter.  The
> second arg need only be any object that is sure to differ from
> what calling the class/type/other callable can result in, to
> ensure the resulting iterator is unbounded.
>

OK, let me say right at the outset that I admit this posting is a deliberate
contribute to this week's "lighten up" theme. Ignore the fact that I type on
a desk that was only last night moved down from the second floor office.
Ignore the twenty azaleas I planted last weekend. Clearly I'm in need of a
life, so to prove I have one please accept the following in the (not
entirely serious) spirit intended. Though I have followed this thread, I
only decided to finally post when I realised I had an opportunity to write
...

Alex:

This is true, but remember that Ralg clearly stated he was being finical.

What you say appears to mean is that iter(type, None), for example, should
return an unbounded iterator of type. I'd be interested in what you have to
say about this:

>>> int
<type 'int'>
>>> int()
0
>>> int(1)
1
>>> iter(int, None)
<callable-iterator object at 0x00779F58>
>>> x = iter(int, None)
>>> x
<callable-iterator object at 0x0077CC98>
>>> x()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'callable-iterator' object is not callable
>>> dir(x)
['__class__', '__delattr__', '__getattribute__', '__hash__', '__init__',
'__iter
__', '__new__', '__reduce__', '__repr__', '__setattr__', '__str__', 'next']

LOC counters might like to observe the same error message by entering

>>> iter(int, None)()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'callable-iterator' object is not callable
>>>

if-you-can't-call-a-callable-iterator-why-is-it-
        -called-a-callable-iterator-ly y'rs  - steve
--

Steve Holden: http://www.holdenweb.com/ ; Python Web Programming:
http://pydish.holdenweb.com/pwp/






More information about the Python-list mailing list