Mar-20-2019, 11:41 AM
(Mar-11-2019, 08:41 AM)perfringo Wrote: Yoriz already gave you an answer. I add advice which may speed up learning process.
If you encounter something you don't understand (or something what is unexpected) I suggest always start with built-in help and documentation.
>>> help(getattr) Help on built-in function getattr in module builtins: 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. (END)Documentation at python.org:
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.
In ipython there is even less typing:
In [1]: getattr? Docstring: 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. Type: builtin_function_or_method
I saw and understand the documentation. But I saw this in builtins.py
def getattr(object, name, default=None): # known special case of 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. """ pass