Note that there are some explanatory texts on larger screens.

plurals
  1. POPolymorphism with callables in Python
    primarykey
    data
    text
    <p>I have an interface class called iResource, and a number of subclasses, each of which implement the "request" method. The request functions use socket I/O to other machines, so it makes sense to run them asynchronously, so those other machines can work in parallel.</p> <p>The problem is that when I start a thread with iResource.request and give it a subclass as the first argument, it'll call the superclass method. If I try to start it with "type(a).request" and "a" as the first argument, I get "" for the value of type(a). Any ideas what that means and how to get the true type of the method? Can I formally declare an abstract method in Python somehow?</p> <p>EDIT: Including code.</p> <pre><code>def getSocialResults(self, query=''): #for a in self.types["social"]: print type(a) tasks = [type(a).request for a in self.types["social"]] argss = [(a, query, 0) for a in self.types["social"]] grabbers = executeChainResults(tasks, argss) return igrabber.cycleGrabber(grabbers) </code></pre> <p>"executeChainResults" takes a list "tasks" of callables and a list "argss" of args-tuples, and assumes each returns a list. It then executes each in a separate thread, and concatenates the lists of results. I can post that code if necessary, but I haven't had any problems with it so I'll leave it out for now.</p> <p>The objects "a" are DEFINITELY not of type iResource, since it has a single constructor that just throws an exception. However, replacing "type(a).request" with "iResource.request" invokes the base class method. Furthermore, calling "self.types["social"][0].request" directly works fine, but the above code gives me: "type object 'instance' has no attribute 'request'".</p> <p>Uncommenting the commented line prints <code>&lt;type 'instance'&gt;</code> several times.</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.
 

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