FAQ
I'm writing something that needs to get method functions from a class by
name.

The trivial path is something like:

funcitem = instance.__class__.__dict__.get('funcname', None)

The issue is that this only works for local methods, not inherited methods.

I can't use something like:

funcitem = instance.funcname

because funcname may not exist in the object.

So, not being motivated enough to crawl through the inhertiance graph to see
if a function is implemented, I was curious as to whether there is some
function that I might be able to call to do the resolution for me. I was
hoping for something more elegant than:

funcitem = eval('instance.funcname')

Seems a touch expensive, but perhaps not. It does seem a little too shotgun
for me, though.

As an extra bonus, is there anyplace one can get all of the function names
associated with an instance?

Thanx!

Will Hartung
(will.hartung at havasint.com)

Search Discussions

  • Fredrik Lundh at Nov 2, 2000 at 7:29 pm

    Will Hartung wrote:
    I'm writing something that needs to get method functions from a class by
    name.
    funcitem = getattr(instance, 'funcname', None)

    </F>
  • Fredrik Lundh at Nov 2, 2000 at 7:39 pm
    Will Hartung wrote
    As an extra bonus, is there anyplace one can get all of the function names
    associated with an instance?
    # builtin-dir-example-2.py
    # from (the eff-bot guide to) The Python Standard Library
    # http://www.pythonware.com/people/fredrik/librarybook.htm

    class A:
    def a(self):
    pass
    def b(self):
    pass

    class B(A):
    def c(self):
    pass
    def d(self):
    pass

    def getmembers(klass, members=None):
    # get a list of all class members
    if members is None:
    members = []
    for k in klass.__bases__:
    getmembers(k, members)
    for m in dir(klass):
    if m not in members:
    members.append(m)
    return members

    print getmembers(A)
    print getmembers(B)
    print getmembers(IOError)

    ## prints:
    ## ['__doc__', '__module__', 'a', 'b']
    ## ['__doc__', '__module__', 'a', 'b', 'c', 'd']
    ## ['__doc__', '__getitem__', '__init__', '__module__', '__str__']

    </F>

    <!-- (the eff-bot guide to) the standard python library:
    http://www.pythonware.com/people/fredrik/librarybook.htm
    -->
  • Alex Martelli at Nov 2, 2000 at 11:36 pm
    "Will Hartung" <will.hartung at havasint.com> wrote in message
    news:8tsboe0rcs at news1.newsguy.com...
    [snip]
    I can't use something like:

    funcitem = instance.funcname

    because funcname may not exist in the object.
    While the already-suggested getattr is surely best in this
    case, I just wanted to point out that just because something
    might fail doesn't mean you can attempt it anyway -- often,
    the 'try, but catch a possible exception' attempt comes in
    very useful. So, as a general approach, though certainly
    not optimal here, do remember that, e.g.,...:

    try: funcitem = instance.funcname
    except AttributeError: funcitem = None

    is roughly equivalent to the more concise & specific:

    funcitem = getattr(instance, 'funcname', None)


    Alex
  • Will Hartung at Nov 4, 2000 at 12:09 am
    Alex Martelli wrote in message <8tsu1v015s at news2.newsguy.com>...
    "Will Hartung" <will.hartung at havasint.com> wrote in message
    news:8tsboe0rcs at news1.newsguy.com...
    [snip]
    I can't use something like:

    funcitem = instance.funcname

    because funcname may not exist in the object.
    While the already-suggested getattr is surely best in this
    case, I just wanted to point out that just because something
    might fail doesn't mean you can attempt it anyway -- often,
    the 'try, but catch a possible exception' attempt comes in
    very useful. So, as a general approach, though certainly
    not optimal here, do remember that, e.g.,...:

    try: funcitem = instance.funcname
    except AttributeError: funcitem = None

    is roughly equivalent to the more concise & specific:

    funcitem = getattr(instance, 'funcname', None)

    Yes, of course. But for my case, it's a completely dynamic lookup. I'm
    constructing method names based on properties, so, I can't do "funcitem =
    instance.funcname" anyway.

    But thanx for the tips!

    Regards,

    Will Hartung
    (will.hartung at havasint.com)
  • Helen Dawson at Nov 5, 2000 at 7:44 am
    For what it's worth, this is the code used in cmd.py,
    which is used by pdb.py, to allow new commands to
    be added just by creating functions that start with
    do_. It uses the try/except method.

    This is from onecmd() in cmd.py:

    try:
    func = getattr(self, 'do_' + cmd)
    except AttributeError:
    return self.default(line)
    return func(arg)


    Will Hartung wrote:
    Alex Martelli wrote in message <8tsu1v015s at news2.newsguy.com>...
    "Will Hartung" <will.hartung at havasint.com> wrote in message
    news:8tsboe0rcs at news1.newsguy.com...
    [snip]
    I can't use something like:

    funcitem = instance.funcname

    because funcname may not exist in the object.
    While the already-suggested getattr is surely best in this
    case, I just wanted to point out that just because something
    might fail doesn't mean you can attempt it anyway -- often,
    the 'try, but catch a possible exception' attempt comes in
    very useful. So, as a general approach, though certainly
    not optimal here, do remember that, e.g.,...:

    try: funcitem = instance.funcname
    except AttributeError: funcitem = None

    is roughly equivalent to the more concise & specific:

    funcitem = getattr(instance, 'funcname', None)
    Yes, of course. But for my case, it's a completely dynamic lookup. I'm
    constructing method names based on properties, so, I can't do "funcitem =
    instance.funcname" anyway.

    But thanx for the tips!

    Regards,

    Will Hartung
    (will.hartung at havasint.com)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 2, '00 at 6:28p
activeNov 5, '00 at 7:44a
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase