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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:44:04+00:00 2026-05-11T06:44:04+00:00

How do I write a Python class that handles calls on undefined methods by

  • 0

How do I write a Python class that handles calls on undefined methods by first, getting the output of a function of the same name from a given module, and then, doing something further with that output?

For example, given add(x, y), doublerInstance.add(1, 1) should return 4.

I know _ _ getattr _ _() intercepts calls on undefined methods, and getattr() can retrieve a function object. But I don’t know how get the arguments passed to the undefined call caught by _ _ getattr _ _() to the function retrieved by getattr().

EXAMPLE Module functions.py: def add(x, y):     return x +   Module doubler.py: class Doubler:   def __init__(self, source):     self.source = source    def __getattr__(self, attrname):     fnc = getattr(self.source, attrname)     return fnc() * 2  Session: >import functions as f >import doubler as d >doublerInstance = d.Doubler(f) >doublerInstance.add(1, 2) <snip> TypeError: add() takes exactly 2 arguments, (0 given)  END 

I do understand the error — getattr() returns a function to be run, and the call fnc() doesn’t pass any arguments to that function — here, add(). But how do I get the arguments passed in to the call dblr.add(1, 2) and pass those to the function returned by the getattr() call?

I’m looking for the right way to do this, not some usage of _ _ getattr _ _. I realize that decorator functions using @ might be a better tool here, but I don’t yet understand those well enough to see whether they could be applied here.

ALSO — what resource should I have looked at to figure this out for myself? I haven’t found it in the Lutz books, the Cookbook, or the Python Library Reference.

  • 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. 2026-05-11T06:44:04+00:00Added an answer on May 11, 2026 at 6:44 am

    When you call doublerInstance.add(1, 2), you’re getting an attribute add from it, and then you’re calling it. But inside your getattr, you’re returning a value. You have to return a function.

    Anyway, for this particular case to work, you need this:

    def __getattr__(self, attrname) :     fnc = getattr(self.source, attrname)     def doubled(*args, **kwargs) :         return 2 * fnc(*args, **kwargs)     return doubled 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

I've seen how to overload + and * in Ruby, so that my_obj +
I have been trying to use an ajax-style file upload component (like the dojox
Is there a Python class that wraps the file interface (read, write etc.) around
I have foreach function which calls specified function on every element which it contains.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.