Трассировка должна предупредить вас о доступе к атрибуту, вызвавшему исключение AttributeError
:
>>> f.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute 'b'
Также можно преобразовать Exception
в str
:
>>> try:
... f.b
... except AttributeError, e:
... print e
...
Foo instance has no attribute 'b'
Если вы хотите получить список атрибутов, доступных для объекта, попробуйте dir()
или help()
>>> dir(f)
['__doc__', '__init__', '__module__', 'a']
>>> help(str)
Help on class str in module __builtin__:
class str(basestring)
| str(object) -> string
|
| Return a nice string representation of the object.
| If the argument is a string, the return value is the same object.
|
| Method resolution order:
| str
| basestring
| object
|
| Methods defined here:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
[...]
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
Вы даже можете позвонить help()
на dir
(почему это оставлено в качестве упражнения для читателя):
>>> help(dir)
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
В противном случае вы всегда можете взглянуть на код, если только вам не был предоставлен какой-то предварительно скомпилированный модуль третьей стороной, и в этом случае вам следует запросить более качественную документацию (скажем, некоторые модульные тесты!) У вашего поставщика!