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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:30:53+00:00 2026-06-14T05:30:53+00:00

I currently have this code: class Generator(object): def __getattr__(self, name): def f(self): return (Result

  • 0

I currently have this code:

class Generator(object):
    def __getattr__(self, name):    
        def f(self):
            return ("Result of"+name, self)
        f.__name__ = name
        return f

    def foo(self):
        pass

g = Generator()
print g.foo
print Generator.foo
print g.bar
print Generator.bar

Which gives:

<bound method Generator.foo of <__main__.Generator object at 0x00B62D30>>
<unbound method Generator.foo>
<function bar at 0x00A9DE70>
AttributeError: type object 'Generator' has no attribute 'bar'

What do I have to do to make it give:

<bound method Generator.foo of <__main__.Generator object at 0x00B62D30>>
<unbound method Generator.foo>
<bound method Generator.bar of <__main__.Generator object at 0x00B62D30>>
<unbound method Generator.bar>
  • 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-14T05:30:55+00:00Added an answer on June 14, 2026 at 5:30 am

    Here’s a metaclass that adds the __getattr__ function from the class definition back to the metaclass itself. This avoids having to define the function in multiple places, or as a separate global function defined beforehand and added individually to the metaclass and class.

    class Meta(type):
    
        def __new__(mcls, name, bases, dikt):
            fgetattr = dikt.get('__getattr__')
            if fgetattr is not None:
                setattr(mcls, '__getattr__', fgetattr)
            return super(Meta, mcls).__new__(mcls, name, bases, dikt)
    
    class Generator(object):
        __metaclass__ = Meta
    
        def __getattr__(obj, name):
    
            def f(self):
                return "Result of %s for %r" % (name, self)
            f.__name__ = name
    
            if isinstance(obj, type):
                setattr(obj, name, f)
            else:
                setattr(type(obj), name, f)
            return getattr(obj, name)
    

    Rather than directly create the method via the dynamic function’s __get__ descriptor method, I think it’s better to store the function in the class dict and rely on getattr to return the proper bound/unbound method. Subsequent attribute access will use the function from the class. Since the same __getattr__ function is used for both the class and the instance, an isinstance check is required to ensure the dynamic function gets stored to the class and not the instance.

    In Python 3, getting the function as an attribute of the class merely returns the function since unbound methods were removed from the language. Also, the metaclass syntax has changed to a keyword argument in the class definition line.

    Test:

    >>> g = Generator()
    >>> g.foo
    <bound method Generator.foo of <__main__.Generator object at 0xb7248c2c>>
    >>> Generator.foo
    <unbound method Generator.foo>
    >>> g.bar
    <bound method Generator.bar of <__main__.Generator object at 0xb7248c2c>>
    >>> Generator.bar
    <unbound method Generator.bar>
    >>> g.foo()
    'Result of foo for <__main__.Generator object at 0xb7248c2c>'
    >>> Generator.foo(g)
    'Result of foo for <__main__.Generator object at 0xb7248c2c>'
    >>> 'foo' in vars(Generator), 'bar' in vars(Generator)
    (True, True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
Currently I have this code var App = Ember.Application.create(); App.user = Ember.Object.create({ people: customers
I currently have this code that creates 4 tabs using tabactivity: public class toknapp
Currently I have some code like this: OntologyGenerator generator = new OntologyGenerator(); generator.AddOntologyHeader(Testing); generator.AddClassDeclaration(owlBuilder);
I currently have this code which stores XML into an XML-type column called data,
I currently have this code: PACKETS = {}; function AddPacket(data) local id = data.ID;
I currently have this code : Private Sub Worksheet_Change(ByVal Target As Range) Dim lastrow
I currently have this code Private Sub Worksheet_Change(ByVal Target As Range) WorksheetChanged(Target, Range(AB3).CurrentRegion, Range(B18:B19))
I currently have this code which reads the first field in a database record
Ok I currently have this code public int i = 0; //this is outside

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.