Initializing a list with copies

Steve Holden sholden at holdenweb.com
Tue Apr 30 13:46:10 EDT 2002


"Alex Martelli" <aleax at aleax.it> wrote in message
news:sfAz8.82271$vF6.2450688 at news2.tin.it...
> Steve Holden wrote:
>         ...
> > 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
>
> type is not callable without arguments.  iter(XX, None) gives an unbounded
> iterator for any XX which is a type callable without arguments: each call
> to the iterator's .next() yields [aka, each item in the iterator is] a
> "default-initialized" instance of type XX (same as XX() returns).
>
> > if-you-can't-call-a-callable-iterator-why-is-it-
> >         -called-a-callable-iterator-ly y'rs  - steve
>
> "Iterator that wraps a callable" might no doubt be a better name.
However:
>
> Much like a burger flipper is not (necessarily) a burger, so is a callable
> iterator not (necessarily) a callable.  "Callable" here is used as a noun
> (if any noun can be verbed, why can't adjectives be nouned...?).
>


In fact I had kind of made the meta-version of the mistake everyone is being
warned about now, which is to use a type's name as a variable: "type" was
intended to stand as a placemarker for "int", "float", etc., and I
completely overlooked

>>> type(type)
<type 'type'>

so I should probably have typed "iter(atype, None)" or similar. My usual
sloppiness. And, of course, to confound the confusion int() is *not* a call
to the integer class/type creator, but a no-argument call of the built-in
int() function. What happens to "int(x[, radix]) " when int is a built-in
type, by the way, or isn't that going to happen?

Anyway, just to finish up, the step I went wrong on was

>>> x = iter(int, 0)

So I decided instead to create my own type:

>>> class MyObj(object):
...         pass
...
>>> type(MyObj)
<type 'type'>
>>> x = iter(MyObj, None)
>>> x
<callable-iterator object at 0x0079A378>
>>> x()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'callable-iterator' object is not callable # aha!
>>> x.next()
<__main__.MyObj object at 0x007A6450>

So, I have to use the iterator protocol to get something out of this beast,
and since it's unbounded a simple for statement might not be a good idea.

>>> type(x.next())
<class '__main__.MyObj'>
>>> type(MyObj)
<type 'type'>
>>> type(MyObj())
<class '__main__.MyObj'>

That makes perfect sense. So callable-iterators can be used to produce
bounded or unbounded sequences of objects. If the type/class maintains
state, these objects can differ, otherwise they will all be the same (i.e.
each will be the result of calling the type's creator (the callable that was
provided when the iterator was created) with no arguments). Erk, that looks
a bit lisplike.

I was interested to find out that

>>> type()
Traceback ...
TypeError: type() takes 1 or 3 arguments

given the documentation for 2.2 says """
type(object)
Return the type of an object. The return value is a type object. The
standard module types defines names for all built-in types. For instance:
>>> import types
>>> if type(x) == types.StringType: print "It's a string"
"""

so-they-don't-call-it-a-callable-iterator-because-
        -it's-an-iterator-that's-callable-they-call-it-a-
        -callable-iterator-because-it-iterates-callables-
        -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