Dive Into Python Python for experienced programmers |

## 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
    <built-in method pop of list object at 010DF884>
    >>> getattr(li, "pop")
    <built-in method pop of list object at 010DF884>
    >>> getattr(li, "append")("Moe")
    >>> li
    ["Larry", "Curly", "Moe"]
    >>> getattr({}, "clear")
    <built-in method clear of dictionary object at 00F113D4>
    >>> getattr((), "pop")
    Traceback (innermost last):
      File "<interactive input>", line 1, in ?
    AttributeError: 'tuple' object has no attribute 'pop'

| 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.
| 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.
| 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.
| `getattr` also works on dictionaries.
| 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
    <function buildConnectionString at 00D18DD4>
    >>> getattr(odbchelper, "buildConnectionString")
    <function buildConnectionString at 00D18DD4>
    >>> object = odbchelper
    >>> method = "buildConnectionString"
    >>> getattr(object, method)
    <function buildConnectionString at 00D18DD4>
    >>> type(getattr(object, method))
    <type 'function'>
    >>> import types
    >>> type(getattr(object, method)) == types.FunctionType
    1
    >>> callable(getattr(object, method))
    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.)
| 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.
| 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.
| In this case, `method` is the name of a function, which we can prove by getting its `type`.
| Since `method` is a function, it is callable.