{article Dive into Python}{title} {text} {/article}
getattr(object, name[, default])

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

getattr function is used to fetch an attribute from an object, using a string object instead of an identifier to identify the attribute.

getattr(...)
getattr(object, name[, default]) -> value

Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.


li = ["Larry", "Curly"]
li.pop

getattr(li, "pop")
getattr(li, "append") ("moe")
li
getattr({}, "clear")
getattr((), "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 that 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 with Modules

>>> import odbchelper
>>> odbchelper.buildConnectionString
<function buildConnectionString at 0x00000000023BCC80>
>>> getattr(odbchelper, "buildConnectionString")
<function buildConnectionString at 0x00000000023BCC80>
>>> object = odbchelper
>>> method = "buildConnectionString"
>>> getattr(object, method)
<function buildConnectionString at 0x00000000023BCC80>
>>> type(getattr(object, method))
<class 'function'>
>>> import types
>>> type(getattr(object, method)) == types.FunctionType
True
>>> callable(getattr(object, method))
True
>>>

This returns a reference to the buildConnectionString function in the odbchelper module, which you studied in Chapter 2, Your First Python Program. (The hex address you see is specific to my machine; your output will be different.)

Using getattr, you 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 you actually use in the info 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 you can prove by getting its type.

Since method is a function, it is callable

getattr Default Values

import statsout


def output(data, format="text"):
output_function = getattr(statsout, "output_%s" % format, statsout.output_text)
return output_function(data)

This function call is guaranteed to work, because you added a third argument to the call to getattr.
The third argument is a default value that is returned if the attribute or method specified by the second
argument wasn't found.