python - How can I dynamically instantiate (by classname as a string) the class to call getThings() in this example? -
i want see source definition object.__new__() , decided use quick way accessing code. why python giving me typeerror (is not type:method)
, when type()
tells me it's type:method?
(1) >>> import inspect >>> print inspect.getsource(object.__new__) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/inspect.py", line 701, in getsource lines, lnum = getsourcelines(object) file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/inspect.py", line 690, in getsourcelines lines, lnum = findsource(object) file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/inspect.py", line 526, in findsource file = getfile(object) file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/inspect.py", line 420, in getfile 'function, traceback, frame, or code object'.format(object)) typeerror: <built-in method __new__ of type object @ 0x10a2d9410> not module, class, method, function, traceback, frame, or code object >>> type(object.__new__) <type 'builtin_function_or_method'> >>>
the reason wanted see definition in first place because method i'm trying call requires instance of specific class first argument.
(2) typeerror: unbound method getthings() must called funclass instance first argument (got sadclass instance instead)
it worth noting funclass subclass of sadclass, , error message implies, have instance of sadclass. feel have need call getthings()
, i'm wondering if there's clever way instance of funclass, call getthings()
?
i declare instance of need, i'm having trouble because class , function end getting called based on user method input arguments (strings) using getattr()
class object (funclass), , method object (getthings). attempts @ creating instance of funclass ended here:
(3) >>> classobject = getattr(funclass,"funclass") >>> classobject <class 'funclass.funclass'> >>> classobject.__new__() traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: object.__new__(): not enough arguments >>>
ultimately, want able call getthings, suppose requires instance of funclass. have variable classmethod, tried using call method. again, class name , method name come in strings, , using getattr() declare objects, this:
(4) >>> classmethod = getattr(funclass, "getthings") >>> classmethod <unbound method funclass.getthings>
when call classmethod()
, error seen in (4). might have more 1 problem, insight aspect great! thanks.
edit:
(as per @nneonneo 's answer)
i ended doing:
classinstance = globals()["funclass"](self.thing1, self.thing2) classmethod = getattr(classinstance, "getthing") classmethod() # call function
where "funclass" , "getthing" strings passed function, , number of class/method combinations. thing1 , thing2 required instantiate parent class (sadclass) somewhere else, allows me instantiate subclass, "funclass" using existing attributes, , call 1 of funclass's class methods. sadclass has __init__ , funclass not. problem solved.
why calling __new__
directly, @ all? make new instance this:
obj = funclass()
or, use dynamic name, globals()['funclass']()
.
then can do
obj.getthings()
or, dynamically, getattr(obj, 'getthings')()
.
by way, can't see source of object.__new__
because it's built-in method (and implemented @ lower level, e.g. c cpython).
Comments
Post a Comment