Obtaining an member function by name

Discussion in 'Python' started by guy lateur, Nov 19, 2005.

  1. guy lateur

    guy lateur Guest

    Hi all,

    Suppose you have this class:

    class foo:
    def bar():

    Suppose you also have the strings "foo" and "bar". How can you obtain the
    function foo.bar()?

    Surely somebody knows..

    TIA,
    g
    guy lateur, Nov 19, 2005
    #1
    1. Advertising

  2. guy lateur

    Guest

    f = getattr(obj,"bar")
    f()

    guy lateur wrote:
    > Hi all,
    >
    > Suppose you have this class:
    >
    > class foo:
    > def bar():
    >
    > Suppose you also have the strings "foo" and "bar". How can you obtain the
    > function foo.bar()?
    >
    > Surely somebody knows..
    >
    > TIA,
    > g
    , Nov 19, 2005
    #2
    1. Advertising

  3. guy lateur wrote:
    > Hi all,
    >
    > Suppose you have this class:
    >
    > class foo:
    > def bar():
    >
    > Suppose you also have the strings "foo" and "bar". How can you obtain the
    > function foo.bar()?
    >
    > Surely somebody knows..


    getattr helps. However, your example won't work: it misses either a
    staticmethod-declaration, or a self-argument, or a classmethod and
    cls-argument. So unless we know if bar shall be an instance.method or
    not, it's hard to tell what exactly you want. Because you could want

    getattr(getattr(mymodule, "foo"), "bar")

    Or

    getattr(getattr(mymodule, "foo")(), "bar")

    (notice the parentheses)

    or

    getattr(getattr(locals(), "foo"), "bar")

    or

    getattr(getattr(globals(), "foo"), "bar")

    Diez
    Diez B. Roggisch, Nov 19, 2005
    #3
  4. On Sat, 19 Nov 2005 14:12:25 GMT, "guy lateur" <> wrote:

    >Hi all,
    >
    >Suppose you have this class:
    >
    >class foo:
    > def bar():
    >
    >Suppose you also have the strings "foo" and "bar". How can you obtain the
    >function foo.bar()?

    Why don't you type these things into an interactive python session
    and see what happens? Also, foo.bar will be an unbound method of foo,
    not a function per se. You could experiment a little, e.g.,

    >>> class foo:

    ... def bar():
    ...
    File "<stdin>", line 3

    ^
    IndentationError: expected an indented block
    >>> class foo:

    ... def bar(): return 'bar is the name' # you could have done this
    ...
    >>> foo.bar()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unbound method bar() must be called with foo instance as first argument (got nothing
    instead)
    >>> foo()

    <__main__.foo instance at 0x02EF3D8C>
    >>> foo.bar(foo())

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: bar() takes no arguments (1 given)
    >>> class foo:

    ... def bar(self): return self, 'bar is the name' # you could have done this
    ...
    >>> fooinst = foo()
    >>> fooinst

    <__main__.foo instance at 0x02EF756C>
    >>> foo.bar(fooinst)

    (<__main__.foo instance at 0x02EF756C>, 'bar is the name')
    >>> fooinst.bar

    <bound method foo.bar of <__main__.foo instance at 0x02EF756C>>
    >>> fooinst.bar()

    (<__main__.foo instance at 0x02EF756C>, 'bar is the name')
    >>> foo.bar.im_func

    <function bar at 0x02EF5764>
    >>> foo.bar.im_func('first arg')

    ('first arg', 'bar is the name')
    >>> fooinst.bar

    <bound method foo.bar of <__main__.foo instance at 0x02EF756C>>
    >>> fooinst.bar.im_func

    <function bar at 0x02EF5764>
    >>> fooinst.bar.im_func(1234)

    (1234, 'bar is the name')
    >>> fooinst.bar()

    (<__main__.foo instance at 0x02EF756C>, 'bar is the name')
    >>> foo.bar(333)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unbound method bar() must be called with foo instance as first argument (got int inst
    ance instead)
    >>> foo.bar(fooinst)

    (<__main__.foo instance at 0x02EF756C>, 'bar is the name')

    Someone can explain. If you do some of your own work, it will help even the load.
    Have you looked at any documentation? Start at http://www.python.org/
    and click a few things. There seems to be a beginners guide link under documentation
    in the sidebar to the left ;-)

    Regards,
    Bengt Richter
    Bengt Richter, Nov 19, 2005
    #4
  5. guy lateur

    EuGeNe Guest

    guy lateur wrote:
    > Hi all,
    >
    > Suppose you have this class:
    >
    > class foo:
    > def bar():
    >
    > Suppose you also have the strings "foo" and "bar". How can you obtain the
    > function foo.bar()?
    >
    > Surely somebody knows..
    >
    > TIA,
    > g
    >
    >



    Would that do?

    >>> class foo:

    @staticmethod
    def bar():
    pass


    >>> foo.bar

    <function bar at 0x00B445F0>
    >>>
    EuGeNe, Nov 19, 2005
    #5
  6. On Sat, 19 Nov 2005 14:12:25 GMT, "guy lateur" <> wrote:

    >Hi all,
    >
    >Suppose you have this class:
    >
    >class foo:
    > def bar():
    >
    >Suppose you also have the strings "foo" and "bar". How can you obtain the
    >function foo.bar()?
    >
    >Surely somebody knows..
    >

    Sorry, clean forgot about the strings.

    >>> class foo:

    ... def bar(self): return self, 'bar is the name'
    ...
    >>> # The definition is at global scope, so 'foo' will show up

    ... # in the globals() directory, which we can access with appended ['foo']
    ...
    >>> globals()['foo']

    <class __main__.foo at 0x02EE9C2C>
    >>> #if you want the 'bar' attribute using the string

    ... getattr(globals()['foo'], 'bar')
    <unbound method foo.bar>
    >>> foo.bar

    <unbound method foo.bar>

    Note that getting an attribute does some "binding" magic if the
    attribute has certain qualitites. In this case the bar function
    is associated with the foo class to become an "unbound method"

    Nit: usual convention is to spell class names with leading upper case.
    Then you can e.g. use the lower case same name for an instance of the
    class without confusions. Nit2: using new-style classes, which derive
    from object (or also other bases, but at least object or type) is now
    recommended, so you get the full-fledged attribute machinery that supports
    much of the latest magic. So write the above more like

    class Foo(object):
    def bar(self): return self, 'bar is the name'

    Regards,
    Bengt Richter
    Bengt Richter, Nov 19, 2005
    #6
  7. guy lateur

    guy lateur Guest

    Thanks for the feedback, people.

    I actually only need the "bar" part (instance methods). I added the "foo"
    part to generalize the question without really thinking it through first.
    Still, it has gotten me more information than I ever imagined. So thanks
    again.

    g
    guy lateur, Nov 19, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    592
    John Carson
    Jul 30, 2003
  2. slide_o_mix
    Replies:
    0
    Views:
    410
    slide_o_mix
    Oct 15, 2003
  3. Replies:
    1
    Views:
    375
  4. John Ladasky
    Replies:
    3
    Views:
    72
    John Ladasky
    Nov 17, 2013
  5. Cameron Simpson

    Re: Obtaining "the" name of a function/method

    Cameron Simpson, Nov 18, 2013, in forum: Python
    Replies:
    0
    Views:
    69
    Cameron Simpson
    Nov 18, 2013
Loading...

Share This Page