[Tutor] type objects - 2nd try
Gregor Lingl
glingl at aon.at
Sat Aug 16 00:26:58 EDT 2003
Hi!
I'll try to ask a question which I asked already some days ago,
but nobody took notice of it. I'd be very interested in the answer
and I hope to get one, this time.
1. EXPOSITION
There are "type objects" in python:
>>> str
<type 'str'>
>>> int
<type 'int'>
I'm confronted with them e. g. in this way:
>>> type(1)
<type 'int'>
>>> type("a")
<type 'str'>
I can use them to check types for example in this way:
>>> type(1)==int
True
>>> type(1.0)==int
False
This works even for type-objects themselves:
>>> type(type)
<type 'type'>
>>> type
<type 'type'>
>>> type(type) == type
True
2. OBSERVATION
The type (i.e . the type type) , which is used like a function
outputs several other types:
>>> def f():
... pass
...
>>> type(f)
<type 'function'>
>>> def g():
... yield 1
...
>>> type(g())
<type 'generator'>
>>> type(len)
<type 'builtin_function_or_method'>
BUT there seem to be no type-objects, at least with these names, belonging
to this "types":
>>> function
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'function' is not defined
>>> generator
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'generator' is not defined
So I have to resort to the "old-fashioned" way to determine
types of objects, namely to compare to the types of known
objects:
>>> type(len) == type(abs)
True
>>> type(f)==type(g)
True
>>> def h():
... yield 2
...
>>> x = h()
>>> y = g()
>>> x
<generator object at 0x00E244E0>
>>> y
<generator object at 0x00E24440>
>>> type(x)==type(y)
True
3. QUESTION
(1) Are there named type-objects for functions, generators, classes
and the like, in order to determine the type of functions, generators etc.
more easily, i. e. not to need to define a "prototype"-object first.
If so, what are their names?
(2) Where can I find an in-depth - albei easily to read - discussion
of "type-objects", especially if there are different types of types
("All types are equal, but some are more equal ?)
Thanks in advance,
Gregor
More information about the Tutor
mailing list