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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:22:56+00:00 2026-06-12T19:22:56+00:00

It is possible to grab an object attribute using either getattr(obj, attr) or inspect.getmembers(obj)

  • 0

It is possible to grab an object attribute using either getattr(obj, attr) or inspect.getmembers(obj) and then filtering by name:

import inspect

class Foo(object):

    def __init__(self):

        self.a = 100

    def method(self): pass

foo = Foo()
method_by_getattr = getattr(foo, 'method')

foo_members = inspect.getmembers(foo) 
method_by_inspect = [member[1] for member in foo_members 
                        if member[0] == "method"][0]

print (id(method_by_getattr), method_by_getattr, type(method_by_getattr))
print (id(method_by_inspect), method_by_inspect, type(method_by_inspect))

a_by_getattr = getattr(foo, "a")
a_by_inspect = [member[1] for member in foo_members
                        if member[0] == "a"][0]

print (id(a_by_getattr), a_by_getattr, type(a_by_getattr))
print (id(a_by_inspect), a_by_inspect, type(a_by_inspect))

# (38842160L, <bound method Foo.method of <__main__.Foo object at 0x00000000025EF390>>, <type 'instancemethod'>)
# (39673576L, <bound method Foo.method of <__main__.Foo object at 0x00000000025EF390>>, <type 'instancemethod'>)
# (34072832L, 100, <type 'int'>)
# (34072832L, 100, <type 'int'>)

For the ‘a’ attribute getattr and inspect.getmembers are returning the same object. But for the method ‘method’ they are returning different objects (as can be seen by the disparate id’s).

Why is this so?

  • 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-12T19:22:57+00:00Added an answer on June 12, 2026 at 7:22 pm

    I restructured your example a little to better illustrate the variants I’ll use to explain the behaviour

    With temporary variables

    import inspect
    
    def print_id(obj):
        print "{} => {}".format(id(obj), obj)
    
    def getmember(obj, name):
        #members = dict(inspect.getmembers(obj))
        #return members[name]
        return [member 
                for _name, member in inspect.getmembers(obj) 
                if name == _name][0]
    
    class Foo(object):
        def bar(self): pass
    
    foo = Foo()
    
    m1 = foo.bar
    m2 = getattr(foo, 'bar')
    m3 = getmember(foo, 'bar')
    
    print_id(m1)
    print_id(m2)
    print_id(m3)
    

    However, if you inspect objects in a REPL, the basic structure of your code will probably look like this:

    Without temporary variables

    #...
    foo = Foo()
    
    print_id(foo.bar)
    print_id(getattr(foo, 'bar'))
    print_id(getmember(foo, 'bar'))
    

    The id() function basically returns the memory address of an object. That is, it’s not an identity that’s unique between all objects created during the whole runtime of a program. It’s only unique between all the objects that exist in the process at any given point in time.

    The explanation that fits the difference between the two examples is that resolving foo.bar in any of the three ways gives you a new object every time. In the first example, these objects are stored in temporary variables, so all three have to be located at different memory addresses.

    In the second example, the bound method object is no longer needed after it’s printed out; the Python reference counting GC will free its memory. This means that the next time the bound method object is created, it’s a new object that happens to get created at the same memory address as the previous one. This is why it can seem like you’re getting the same object multiple times.

    That you always get a new bound method object can be shown trivially:

    >>> foo.bar == foo.bar
    True
    >>> foo.bar is foo.bar
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to grab the indices/vertices from an XNA model object? I want
Using the PHP IMAP library, is it possible to grab just the first 100
Is it possible to grab a list of memcached key based on some regex?
Is it possible in any of the modern SCMs to grab a complete list
Not sure if this is possible. I want to grab the latest X stories
With a windows MSI file, is there a way to grab all the possible
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Is it possible to grab an event, hold it in a class property, do
Is it possible to grab data from two tables (that have the same fields)
Is it possible to grab the AST of a block from Ruby itself? I've

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.