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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:33:44+00:00 2026-06-15T17:33:44+00:00

Consider the following code example (python 2.7): class Parent: def __init__(self, child): self.child =

  • 0

Consider the following code example (python 2.7):

class Parent:
    def __init__(self, child):
        self.child = child

    def __getattr__(self, attr):
        print("Calling __getattr__: "+attr)
        if hasattr(self.child, attr):
            return getattr(self.child, attr)
        else:
            raise AttributeError(attr)

class Child:
    def make_statement(self, age=10):
        print("I am an instance of Child with age "+str(age))

kid = Child()
person = Parent(kid)

kid.make_statement(5)
person.make_statement(20)

it can be shown, that the function call person.make_statement(20) calls the Child.make_statement function through the Parent‘s __getattr__ function. In the __getattr__ function I can print out the attribute, before the corresponding function in the child instance is called. So far so clear.

But how is the argument of the call person.make_statement(20) passed through __getattr__? How am I able to print out the number ’20’ in my __getattr__ function?

  • 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-06-15T17:33:45+00:00Added an answer on June 15, 2026 at 5:33 pm

    You are not printing 20 in your __getattr__ function. The function finds the make_statement attribute on the Child instance and returns that. As it happens, that attribute is a method, so it is callable. Python thus calls the returned method, and that method then prints 20.

    If you were to remove the () call, it would still work; we can store the method and call it separately to get 20 printed:

    >>> person.make_statement
    Calling __getattr__: make_statement
    <bound method Child.make_statement of <__main__.Child instance at 0x10db5ed88>>
    >>> ms = person.make_statement
    Calling __getattr__: make_statement
    >>> ms()
    I am an instance of Child with age 10
    

    If you have to see the arguments, you’d have to return a wrapper function instead:

    def __getattr__(self, attr):
        print("Calling __getattr__: "+attr)
        if hasattr(self.child, attr):
            def wrapper(*args, **kw):
                print('called with %r and %r' % (args, kw))
                return getattr(self.child, attr)(*args, **kw)
            return wrapper
        raise AttributeError(attr)
    

    This now results in:

    >>> person.make_statement(20)
    Calling __getattr__: make_statement
    called with (20,) and {}
    I am an instance of Child with age 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Consider the following code: class MyCustomDescriptor: def __init__(self,foo): self._foo = foo def __call__(self,decorated_method): #
Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class
Consider the following code: >>> class A(object): ... pass ... >>> def __repr__(self): ...
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following example: #include Python.h #include <boost/python.hpp> #include <boost/shared_ptr.hpp> class A {}; class
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt

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.