New inited instance of class?

Francis Avila francisgavila at yahoo.com
Sat Dec 6 23:30:20 EST 2003


Samuel Kleiner wrote in message ...
>Not for me. I really want to call it as the function itself, and
>
>type(self)(argument1,argument2,argument3)
>
>fails with
>
>Traceback (most recent call last):
>  File "<stdin>", line 139, in ?
>  File "<stdin>", line 78, in __add__
>TypeError: instance() takes at most 2 arguments (3 given)

Ah! You're using classic classes.  Don't do that.

Observe:

>>> class classic: pass
>>> type(classic)
<type 'classobj'>
>>> type(classic())
<type 'instance'>
>>> classic().__class__
<class __main__.classic at 0x00F58030>

>>> class newstyle(object): pass
>>> type(newstyle)
<type 'type'>
>>> type(newstyle())
<class '__main__.newstyle'>
>>> newstyle().__class__
<class '__main__.newstyle'>
>>>

If you're curious, look in the Python Language Reference at the old and new
style classes to see the differences.  There's absolutely no advantage to
old style classes, so stop usin' 'em.

There's a grand old intro (by Guido) to new-style classes at python.org,
linked from "What's New" in the 2.3 docs.  I keep hunting for that link: it
really should be in the distributed docs, because it's vital for
understanding the still poorly-documented new-style classes.
--
Francis Avila





More information about the Python-list mailing list