2.4. Getting object references with getattr

You already know that Python functions are objects. What you don’t know is that you can get a reference to a function without knowing its name until run-time, using the getattr function.

Example 2.11. Introducing getattr

>>> li = ["Larry", "Curly"]
>>> li.pop                       1
<built-in method pop of list object at 010DF884>
>>> getattr(li, "pop")           2
<built-in method pop of list object at 010DF884>
>>> getattr(li, "append")("Moe") 3
>>> li
["Larry", "Curly", "Moe"]
>>> getattr({}, "clear")         4
<built-in method clear of dictionary object at 00F113D4>
>>> getattr((), "pop")           5
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'pop'
1 This gets a reference to the pop method of the list. Note that this is not calling the pop method; that would be li.pop(). This is the method itself.
2 This also returns a reference to the pop method, but this time, the method name is specified as a string argument to the getattr function. getattr is an incredibly useful built-in function which returns any attribute of any object. In this case, the object is a list, and the attribute is the pop method.
3 In case it hasn’t sunk in just how incredibly useful this is, try this: the return value of getattr is the method, which you can then call just as if you had said li.append("Moe") directly. But you didn’t call the function directly; you specified the function name as a string instead.
4 getattr also works on dictionaries.
5 In theory, getattr would work on tuples, except that tuples have no methods, so getattr will raise an exception no matter what attribute name you give.

getattr isn’t just for built-in datatypes. It also works on modules.

Example 2.12. getattr in apihelper.py

>>> import odbchelper
>>> odbchelper.buildConnectionString             1
<function buildConnectionString at 00D18DD4>
>>> getattr(odbchelper, "buildConnectionString") 2
<function buildConnectionString at 00D18DD4>
>>> object = odbchelper
>>> method = "buildConnectionString"
>>> getattr(object, method)                      3
<function buildConnectionString at 00D18DD4>
>>> type(getattr(object, method))                4
<type 'function'>
>>> import types
>>> type(getattr(object, method)) == types.FunctionType
>>> callable(getattr(object, method))            5
1 This returns a reference to the buildConnectionString function in the odbchelper module, which we studied in Getting To Know Python. (The hex address you see is specific to my machine; your output will be different.)
2 Using getattr, we can get the same reference to the same function. In general, getattr(object, "attribute") is equivalent to object.attribute. If object is a module, then attribute can be anything defined in the module: a function, class, or global variable.
3 And this is what we actually use in the help function. object is passed into the function as an argument; method is a string which is the name of a method or function.
4 In this case, method is the name of a function, which we can prove by getting its type.
5 Since method is a function, it is callable.