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

  • Home
  • SEARCH
  • 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 7039825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:50:42+00:00 2026-05-28T01:50:42+00:00

According to the docs, super(cls, obj) returns a proxy object that delegates method calls

  • 0

According to the docs, super(cls, obj) returns

a proxy object that delegates method calls to a parent or sibling
class of type cls

I understand why super() offers this functionality, but I need something slightly different: I need to create a proxy object that delegates methods calls (and attribute lookups) to class cls itself; and as in super, if cls doesn’t implement the method/attribute, my proxy should continue looking in the MRO order (of the new not the original class). Is there any function I can write that achieves that?

Example:

class X:
  def act():
    #...

class Y:
  def act():
    #...

class A(X, Y):
  def act():
    #...

class B(X, Y):
  def act():
    #...

class C(A, B):
  def act():
    #...

c = C()
b = some_magic_function(B, c)
# `b` needs to delegate calls to `act` to B, and look up attribute `s` in B
# I will pass `b` somewhere else, and have no control over it

Of course, I could do b = super(A, c), but that relies on knowing the exact class hierarchy and the fact that B follows A in the MRO. It would silently break if any of these two assumptions change in the future. (Note that super doesn’t make any such assumptions!)

If I just needed to call b.act(), I could use B.act(c). But I am passing b to someone else, and have no idea what they’ll do with it. I need to make sure it doesn’t betray me and start acting like an instance of class C at some point.

A separate question, the documentation for super() (in Python 3.2) only talks about its method delegation, and does not clarify that attribute lookups for the proxy are also performed the same way. Is it an accidental omission?

EDIT

The updated Delegate approach works in the following example as well:

class A:
    def f(self):
        print('A.f')
    def h(self):
        print('A.h')
        self.f()

class B(A):
    def g(self):
        self.f()
        print('B.g')
    def f(self):
        print('B.f')
    def t(self):
        super().h()


a_true = A()
# instance of A ends up executing A.f
a_true.h()

b = B()
a_proxy = Delegate(A, b)
# *unlike* super(), the updated `Delegate` implementation would call A.f, not B.f
a_proxy.h()

Note that the updated class Delegate is closer to what I want than super() for two reasons:

  1. super() only does it proxying for the first call; subsequent calls will happen as normal, since by then the object is used, not its proxy.

  2. super() does not allow attribute access.

Thus, my question as asked has a (nearly) perfect answer in Python.

It turns out that, at a higher level, I was trying to do something I shouldn’t (see my comments here).

  • 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-28T01:50:42+00:00Added an answer on May 28, 2026 at 1:50 am

    This class should cover the most common cases:

    class Delegate:
        def __init__(self, cls, obj):
            self._delegate_cls = cls
            self._delegate_obj = obj
        def __getattr__(self, name):
            x = getattr(self._delegate_cls, name)
            if hasattr(x, "__get__"):
                return x.__get__(self._delegate_obj)
            return x
    

    Use it like this:

    b = Delegate(B, c)
    

    (with the names from your example code.)

    Restrictions:

    1. You cannot retrieve some special attributes like __class__ etc. from the class you pass in the constructor via this proxy. (This restistions also applies to super.)

    2. This might behave weired if the attribute you want to retrieve is some weired kind of descriptor.

    Edit: If you want the code in the update to your question to work as desired, you can use the foloowing code:

    class Delegate:
        def __init__(self, cls):
            self._delegate_cls = cls
        def __getattr__(self, name):
            x = getattr(self._delegate_cls, name)
            if hasattr(x, "__get__"):
                return x.__get__(self)
            return x
    

    This passes the proxy object as self parameter to any called method, and it doesn’t need the original object at all, hence I deleted it from the constructor.

    If you also want instance attributes to be accessible you can use this version:

    class Delegate:
        def __init__(self, cls, obj):
            self._delegate_cls = cls
            self._delegate_obj = obj
        def __getattr__(self, name):
            if name in vars(self._delegate_obj):
                return getattr(self._delegate_obj, name)
            x = getattr(self._delegate_cls, name)
            if hasattr(x, "__get__"):
                return x.__get__(self)
            return x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android app that uses the AlarmService. According to the docs, I
According to docs of ActiveRecord::Base : ==(comparison_object) Returns true if comparison_object is the same
According to the docs for the Unix screen command , you can configure it
According to the gcc docs , memcmp is not an intrinsic function of GCC.
According to the Thinking Sphinx docs ... Turning on delta indexing does not remove
According to the Python 2.6.5 docs [1], the bsddb module has been deprecated for
According to this discussion , the iphone agreement says that it doesn't allow loading
According to the docs , the builtin string encoding string_escape : Produce[s] a string
According to Apple's docs, Subclasses need not override -[UIView drawRect:] if the subclass is
According to Tomcat docs: The maximum size in bytes of the POST which will

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.