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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:02:48+00:00 2026-05-16T18:02:48+00:00

If I have this: class foo(object): @property def bar(self): return 0 f = foo()

  • 0

If I have this:

class foo(object):
    @property
    def bar(self):
        return 0

f = foo()

How do I get a reference to f.bar without actually invoking the method, if this is even possible?

Edited to add: What I want to do is write a function that iterates over the members of f and does something with them (what is not important). Properties are tripping me up because merely naming them in getattr() invokes their __get__() method.

  • 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-16T18:02:49+00:00Added an answer on May 16, 2026 at 6:02 pm

    get_dict_attr (below) looks up attr in a given object’s __dict__, and returns the associated value if its there. If attr is not a key in that __dict__, the object’s MRO’s __dict__s are searched. If the key is not found, an AttributeError is raised.

    def get_dict_attr(obj, attr):
        for obj in [obj] + obj.__class__.mro():
            if attr in obj.__dict__:
                return obj.__dict__[attr]
        raise AttributeError
    

    For example,

    class Foo(object):
        x=1
        def bar(self):
            pass
        @property
        def baz(self):
            return 0
    
    foo=Foo()
    print(get_dict_attr(foo,'x'))
    # 1
    print(get_dict_attr(foo,'bar'))
    # <unbound method Foo.bar>
    print(get_dict_attr(foo,'baz'))
    # <property object at 0xb77c0dc4>
    print(get_dict_attr(foo,'y'))
    # AttributeError
    

    Note that this is very different than the normal rules of attribute lookup.
    For one thing, data-descriptors in obj.__class__.__dict__ (descriptors with both __get__ and __set__ methods) normally have precedence over values in obj.__dict__. In get_dict_attr, obj.__dict__ has precedence.

    get_dict_attr does not try calling __getattr__.

    Finally, get_dict_attr will only work with objects obj which are instances of new-style classes.

    Nevertheless, I hope it is of some help.


    class Foo(object):
        @property
        def bar(self):
            return 0
    
    f = Foo()
    

    This references the property bar:

    print(Foo.bar)
    # <property object at 0xb76d1d9c>
    

    You see bar is a key in Foo.__dict__:

    print(Foo.__dict__['bar'])
    # <property object at 0xb775dbbc>
    

    All properties are descriptors, which implies it has a __get__ method:

    print(Foo.bar.__get__)
    # <method-wrapper '__get__' of property object at 0xb76d7d74>
    

    You can call the method by passing the object f, and the class of f as arguments:

    print(Foo.bar.__get__(f,Foo))
    # 0
    

    I am fond of the following diagram. Vertical lines show the relationship between an object and the object’s class.

    When you have this situation:

       Foo                                B
       | Foo.__dict__={'bar':b}           | B.__dict__={'__get__':...}
       |                      \           |      
       f                       `--------> b
    

    f.bar causes b.__get__(f,Foo) to be called.

    This is explained in detail here.

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

Sidebar

Related Questions

I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):
Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name
If you have the following class: class Foo(object): def __init__(name): self.name = name And
I have a relationship like this class Foo { static hasMany = [bars: Bar,
I have a Rack application that looks like this: class Foo def initialize(app) @app
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I have a object model a bit like this: public class Foo { public

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.