Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7558041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:16:50+00:00 2026-05-30T12:16:50+00:00

I have an interface class called iResource, and a number of subclasses, each of

  • 0

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.

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?

EDIT: Including 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)

“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.

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'”.

Uncommenting the commented line prints <type 'instance'> several times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:16:50+00:00Added an answer on May 30, 2026 at 12:16 pm

    You can just use the bound method object itself:

    tasks = [a.request for a in self.types["social"]]
    #        ^^^^^^^^^
    grabbers = executeChainResults(tasks, [(query, 0)] * len(tasks))
    #                                     ^^^^^^^^^^^^^^^^^^^^^^^^^
    

    If you insist on calling your methods through the base class you could also do it like this:

    from abc import ABCMeta
    from functools import wraps
    
    def virtualmethod(method):
        method.__isabstractmethod__ = True
        @wraps(method)
        def wrapper(self, *args, **kwargs):
            return getattr(self, method.__name__)(*args, **kwargs)
        return wrapper
    
    class IBase(object):
        __metaclass__ = ABCMeta
        @virtualmethod
        def my_method(self, x, y):
            pass
    
    class AddImpl(IBase):
        def my_method(self, x, y):
            return x + y
    
    class MulImpl(IBase):
        def my_method(self, x, y):
            return x * y
    
    items = [AddImpl(), MulImpl()]
    
    for each in items:
        print IBase.my_method(each, 3, 4)
    
    b = IBase()  #  <-- crash
    

    Result:

    7
    12
    Traceback (most recent call last):
      File "testvirtual.py", line 30, in <module>
        b = IBase()
    TypeError: Can't instantiate abstract class IBase with abstract methods my_method
    

    Python doesn’t support interfaces as e.g. Java does. But with the abc module you can ensure that certain methods must be implemented in subclasses. Normally you would do this with the abc.abstractmethod() decorator, but you still could not call the subclasses method through the base class, like you intend. I had a similar question once and I had the idea of the virtualmethod() decorator. It’s quite simple. It essentially does the same thing as abc.abstratmethod(), but also redirects the call to the subclasses method. The specifics of the abc module can be found in the docs and in PEP3119.

    BTW: I assume you’re using Python >= 2.6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an interface called Interface, and a concrete class called Base, which,
I have an interface with a method Validate() and an abstract class that implement
I have a class called Menu.java which is used as the interface for my
I have an interface: public interface IMyInterface { } I have a class which
If I have a custom class called Tires: #import <Foundation/Foundation.h> @interface Tires : NSObject
I have a user control which contains a textbox. I have a class called
I have an interface called IAddress, and a class called Address that handles street,
I have a class called Node and another class called ClassicNode which extends Node.
Say I have a class called A, and in A there's a method called
I have a class called ListViewController which has a UITableView attached to it and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.