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 757645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:23:16+00:00 2026-05-14T15:23:16+00:00

I want to add methods (more specifically: method aliases) automatically to Python subclasses. If

  • 0

I want to add methods (more specifically: method aliases) automatically to Python subclasses. If the subclass defines a method named ‘get’ I want to add a method alias ‘GET’ to the dictionary of the subclass.

To not repeat myself I’d like to define this modifation routine in the base class. But if I check in the base class __init__ method, there is no such method, since it is defined in the subclass. It will become more clear with some source code:

class Base:

    def __init__(self):
        if hasattr(self, "get"):
            setattr(self, "GET", self.get)


class Sub(Base):

    def get():
        pass


print(dir(Sub))

Output:

['__doc__', '__init__', '__module__', 'get']

It should also contain 'GET'.

Is there a way to do it within the base class?

  • 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-14T15:23:16+00:00Added an answer on May 14, 2026 at 3:23 pm

    Your class’s __init__ method adds a bound method as an attribute to instances of your class. This isn’t exactly the same as adding the attribute to the class. Normally, methods work by storing functions in the class, as attributes, and then creating method objects as these functions are retrieved as attributes from either the class (creating unbound methods which only know the class they belong to) or the instance (creating bound methods, which know their instance.)

    How does that differ from what you’re doing? Well, you assign to the GET instance attribute of a specific instance, not the class. The bound method becomes part of the instance’s data:

    >>> s.__dict__
    {'GET': <bound method Sub.get of <__main__.Sub object at 0xb70896cc>>}
    

    Notice how the method is there under the key GET, but not under get. GET is an instance attribute, but get is not. This is subtly different in a number of ways: the method doesn’t exist in the class object, so you can’t do Sub.GET(instance) to call Sub‘s GET method, even though you can do Sub.get(instance). Secondly, if you have a subclass of Sub that defines its own GET method but not its own get method, the instance attribute would hide the subclass GET method with the bound get method from the baseclass. Thirdly it creates a circular reference between the bound method and the instance: the bound method has a reference to the instance, and the instance now stores a reference to the bound method. Normally bound methods are not stored on the instance partly to avoid that. Circular references are usually not a big issue, because we nowadays have the cyclic-gc module (gc) that takes care of them, but it can’t always clean up reference cycles (for instance, when your class also has a __del__ method.) And lastly, storing bound method objects generally makes your instances unserializable: most serializers (such as pickle) can’t handle bound methods.

    You may not care about any of these issues, but if you do, there’s a better approach to what you’re trying to do: metaclasses. Instead of assigning bound methods to instance attributes as you create instances, you can assign normal functions to class attributes as you create the class:

    class MethodAliasingType(type):
        def __init__(self, name, bases, attrs):
            # attrs is the dict of attributes that was used to create the
            # class 'self', modifying it has no effect on the class.
            # So use setattr() to set the attribute.
            for k, v in attrs.iteritems():
                if not hasattr(self, k.upper()):
                    setattr(self, k.upper(), v)
            super(MethodAliasingType, self).__init__(name, bases, attrs)
    
    class Base(object):
        __metaclass__ = MethodAliasingType
    
    class Sub(Base):
        def get(self):
            pass
    

    Now, Sub.get and Sub.GET really are aliases, and overriding the one and not the other in a subclass works as expected.

    >>> Sub.get
    <unbound method Sub.get>
    >>> Sub.GET
    <unbound method Sub.get>
    >>> Sub().get
    <bound method Sub.get of <__main__.Sub object at 0xb708978c>>
    >>> Sub().GET
    <bound method Sub.get of <__main__.Sub object at 0xb7089a6c>>
    >>> Sub().__dict__
    {}
    

    (Of course, if you don’t want overriding the one and not the other to work, you can simply make this an error in your metaclass.) You can do the same thing as the metaclass using class decorators (in Python 2.6 and later), but it would mean requiring the class decorator on every subclass of Base — class decorators aren’t inherited.

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

Sidebar

Related Questions

I'm using Ruby 1.9.3 on Windows. So I want to add methods to be
So I want to be able to add/remove class methods at runtime. Before you
Is it possible to add properties and special methods to modules? I want to
I want to add a custom model method to the admin filter, however it
In my web application I want to add authorization in certain action method in
The method Picture receives a random int i . I want to add i
Expanding on recent_posts_on_self below, I want to add an all_recent_posts_on_self method but I'm not
Greetings, I have a method to capture packets. After capturing I want to add
I wrote this n-array tree class. I want to write a method to add
Assume you want to add some methods to all Iterables. That can look like

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.