Johnny wrote: > Hi, > > I wonder what is the difference between the built-in function > getattr() and the normal call of a function of a class. Here is the > details: > > getattr( object, name[, default]) > > Return the value of the named attributed 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. > > Is that to say the only difference between the two is that no > matter the specific function exists or not the built-in func will > always return a value, but "class.function" will not?
No, getattr(object, name) will raise an AttributeError if the attribute doesn't exist; no value will be returned. When getattr(object, name, default) is called, then a value will be returned regardless of whether or not the attribute exists (but possibly something else may go wrong). In terms of practice, the getattr() form allows you to specify the attribute at run-time rather than write-time. You can do things like iterate over a list of attributes. -- Robert Kern [EMAIL PROTECTED] "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list