how to create instances of classes without calling the constructor?

castironpi at gmail.com castironpi at gmail.com
Tue Mar 18 02:33:02 CET 2008


On Mar 17, 7:08 pm, "Dominik Jain" <removetoreply.dj... at gmx.net>
wrote:
> Hi!
>
> Does anyone know how an instance of a (new-style) class can be created
> without having to call the constructor (and providing the arguments it
> requires)? With old-style classes, this was possible using new.instance.
> Surely there must be a simple way to do this with new-style classes, too --  
> or else how does pickle manage?
>
> Best,
> Dominik

>>> class C: pass
...
>>> object.__new__( C )
<__main__.C object at 0x00B5ED70>

And for the record:

>>> class C:
...     def __init__( self ):
...             print( 'in init' )
...
>>> C()
in init
<__main__.C object at 0x00B5ED90>
>>> object.__new__( C )
<__main__.C object at 0x00B5EE30>

>>> type( C.__name__, (), dict( C.__dict__ ) )()
in init
<__main__.C object at 0x00B5ED90>

Which is kind of biza-a-r-rre.



More information about the Python-list mailing list