oop - class name as variable in python -
i remember following code in c++:
myobj = myclass(); typedef typeof(myobj) newclass; newclass newobj = newclass();
then myobj
, newobj
kind of myclass.
now need write function
in python
, pass myobject
function
, new call constructor
of myobject
. have many class
.
question: how it?
this creates reference myclass:
>>> class myclass(object): ... pass ... >>> myobj = myclass() >>> newclass = myobj.__class__ >>> newobj = newclass() >>> myobj, newobj (<__main__.myclass object @ 0x102740d90>, <__main__.myclass object @ 0x102740d50>)
this creates new class based on myobj's class:
>>> myobj = myclass() >>> newclass = type("newclass", (myobj.__class__,), {}) >>> newobj = newclass() >>> myobj, newobj (<__main__.myclass object @ 0x102740d90>, <__main__.newclass object @ 0x102752610>) >>>
Comments
Post a Comment