Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I dynamically instantiate (by classname as a string) the class to call getThings() in this example?
    primarykey
    data
    text
    <p>I want to see the source definition for object.__new__() and decided to use this quick way for accessing that code. Why is python giving me a <code>TypeError (is not a type:method)</code>, when <code>type()</code> tells me it's a type:method? </p> <pre><code>(1) &gt;&gt;&gt; import inspect &gt;&gt;&gt; print inspect.getsource(object.__new__) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; 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: &lt;built-in method __new__ of type object at 0x10a2d9410&gt; is not a module, class, method, function, traceback, frame, or code object &gt;&gt;&gt; type(object.__new__) &lt;type 'builtin_function_or_method'&gt; &gt;&gt;&gt; </code></pre> <p>The reason I wanted to see the definition in the first place is because the method I'm trying to call requires an instance of a specific class as its first argument. </p> <pre><code>(2) TypeError: unbound method getThings() must be called with FunClass instance as first argument (got SadClass instance instead) </code></pre> <p>It is also worth noting that FunClass is a subclass of SadClass, and as the error message implies, I already have an instance of SadClass. I feel like I have everything I need to call <code>getThings()</code>, but I'm wondering if there's a clever way to get an instance of FunClass, with which I could call <code>getThings()</code>?</p> <p>I would just declare an instance of what I need, but I'm having trouble because the class and function that end up getting called are based on user method input arguments (strings) which I was using <code>getattr()</code> to get a class object (FunClass), and a method object (getThings). My attempts at creating an instance of FunClass ended up here:</p> <pre><code>(3) &gt;&gt;&gt; ClassObject = getattr(FunClass,"FunClass") &gt;&gt;&gt; ClassObject &lt;class 'FunClass.FunClass'&gt; &gt;&gt;&gt; ClassObject.__new__() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: object.__new__(): not enough arguments &gt;&gt;&gt; </code></pre> <p>Ultimately, I just want to be able to call getThings, which I suppose requires an instance of FunClass. I also have a variable ClassMethod, which I tried using to call the method. Again, my class name and method name come in as strings, and I was using getattr() to declare objects, like this:</p> <pre><code>(4) &gt;&gt;&gt; ClassMethod = getattr(FunClass, "getThings") &gt;&gt;&gt; ClassMethod &lt;unbound method FunClass.getThings&gt; </code></pre> <p>When I call <code>ClassMethod()</code>, I get the error seen in (4). I might have more than one problem, but insight to any aspect would be great! Thanks.</p> <p>EDIT:</p> <p>(as per @nneonneo 's answer)</p> <p>I ended up doing:</p> <pre><code>ClassInstance = globals()["FunClass"](self.thing1, self.thing2) ClassMethod = getattr(ClassInstance, "getThing") ClassMethod() # to call the function </code></pre> <p>where "FunClass" and "getThing" are actually strings passed into the function, and could be any number of class/method combinations. Thing1 and Thing2 are required to instantiate the parent class (SadClass) somewhere else, so this allows me to instantiate its subclass, "FunClass" using those existing attributes, and then call one of FunClass's class methods. SadClass has the __init__ and FunClass does not. Problem solved. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload