tags:

views:

66

answers:

5

I'm trying to get the name of all methods in my class. When testing how the inspect module works, i extraced one of my methods by obj = MyClass.__dict__['mymethodname'].

But now inspect.ismethod(obj) returns False while inspect.isfunction(obj) returns True, and i don't understand why. Is there some strange way of marking methods as methods that i am not aware of? I thought it was just that it is defined in the class and takes 'self' as its first argument.

+1  A: 

Use the source

def ismethod(object):
    """Return true if the object is an instance method.

    Instance method objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this method was defined
        im_class        class object in which this method belongs
        im_func         function object containing implementation of method
        im_self         instance to which this method is bound, or None"""
    return isinstance(object, types.MethodType)

The first argument being self is just by convention. By accessing the method by name from the Class's dict, you are bypassing the binding, so it appears to be a function rather than a method

If you want to access the method by name use

getattr(MyClass, 'mymethodname') 
gnibbler
+1 for mentioning `getattr`
atzz
A: 

You could use dir to get the name of available methods/attributes/etc, then iterate through them to see which ones are methods. Like this:

[ mthd for mthd in dir(FooClass) if inspect.ismethod(myFooInstance.__getattribute__(mthd)) ]

I'm expecting there to be a cleaner solution, but this could be something you could use if nobody else comes up with one. I'd like if I didn't have to use an instance of the class to use getattribute.

orangeoctopus
+1  A: 

Well, do you mean that obj.mymethod is a method (with implicitly passed self) while Klass.__dict__['mymethod'] is a function?

Basically Klass.__dict__['mymethod'] is the "raw" function, which can be turned to a method by something called descriptors. This means that every function on a class can be both a normal function and a method, depending on how you access them. This is how the class system works in Python and quite normal.

If you want methods, you can't go though __dict__ (which you never should anyways). To get all methods you should do inspect.getmembers(Klass_or_Instance, inspect.ismethod)

You can read the details here, the explanation about this is under "User-defined methods".

THC4k
When i am making a class which inherits from a built-in python class, is there a way to only get the user-defined methods? Looking at `__dict__` seems to work, but you say one shouldn't do that. Is there another way?
Eskil
@Eskil - Looking at `__dict__` won't work with classes that don't have `__dict__` ("new-style classes" don't have it by default), or will work incorrectly with classes that do have it but do not reflect their attributes in `__dict__` (as may be the case with classes defined by extensions and with some built-in classes). Use `inspect.getmembers`, as shown above.
atzz
But inspect.getmembers(Klass_or_Instance, inspect.ismethod) , as above, gives all methods and not just user-defined methods.Is it safe to look at method.__module__ to see if it matches my own module?
Eskil
It looks at a object and gives you all members that are methods (duh). What do you actually want? Only members defined on the given class?
THC4k
+3  A: 

You are seeing some effects of the behind-the-scenes machinery of Python.

When you write f = MyClass.__dict__['mymethodname'], you get the raw implementation of "mymethodname", which is a plain function. To call it, you need to pass in an additional parameter, class instance.

When you write f = MyClass.mymethodname (note the absence of parentheses after mymethodname), you get an unbound method of class MyClass, which is an instance of MethodType that wraps the raw function you obtained above. To call it, you need to pass in an additional parameter, class instance.

When you write f = MyClass().mymethodname (note that i've created an object of class MyClass before taking its method), you get a bound method of an instance of class MyClass. You do not need to pass an additional class instance to it, since it's already stored inside it.

To get wrapped method (bound or unbound) by its name given as a string, use getattr, as noted by gnibbler. For example:

unbound_mth = getattr(MyClass, "mymethodname")

or

bound_mth = getattr(an_instance_of_MyClass, "mymethodname")
atzz
A: 

From a comment made on @THC4k's answer, it looks like the OP wants to discriminate between built-in methods and methods defined in pure Python code. User defined methods are of types.MethodType, but built-in methods are not.

You can get the various types like so:

import inspect
import types

is_user_defined_method = inspect.ismethod

def is_builtin_method(arg):
    return isinstance(arg, (type(str.find), type('foo'.find)))

def is_user_or_builtin_method(arg):
    MethodType = types.MethodType
    return isinstance(arg, (type(str.find), type('foo'.find), MethodType))

class MyDict(dict):
    def puddle(self): pass

for obj in (MyDict, MyDict()):
    for test_func in (is_user_defined_method, is_builtin_method,
            is_user_or_builtin_method):
        print [attr 
            for attr in dir(obj)
            if test_func(getattr(obj, attr)) and attr.startswith('p')]

which prints:

['puddle']
['pop', 'popitem']
['pop', 'popitem', 'puddle']
['puddle']
['pop', 'popitem']
['pop', 'popitem', 'puddle']
Matt Anderson
Ah yes this is what i would have needed if i was making classes inheriting from "pure" python classes. But lets say i want to make a child class of unittest.TestCase, and want to distinguish between my own new methods and the TestCase methods? This doesn't work in that case, since both types return True from inspect.ismethod.
Eskil