Are there any details about the AttributeError exception in Python?

I am trying to call a function. One parameter is a variable with attributes (which I know due to the AttributeError exception that I received). I do not know the exact attributes this variable should have, so I was wondering if there was any way to see some additional information about the exception, for example, which attribute it could not find. Thanks.

+6
python exception error-handling attributes
source share
3 answers

AttributeError usually identifies a missing attribute. eg:.

 class Foo: def __init__(self): self.a = 1 f = Foo() print(fa) print(fb) 

When I run this, I see:

 $ python foo.py 1 Traceback (most recent call last): File "foo.py", line 10, in <module> print(fb) AttributeError: Foo instance has no attribute 'b' 

This is pretty clear. If you do not see something like this, send the exact error you see.

EDIT

If you need to force an exception to be printed (for any reason), you can do this:

 import traceback try: # call function that gets AttributeError except AttributeError: traceback.print_exc() 

This should give you a complete error message and trace related to the exception.

+13
source share

The following should warn you of access to the attribute that caused the AttributeError exception:

 >>> fb Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Foo instance has no attribute 'b' 

Alternatively, convert Exception to str :

 >>> try: ... fb ... except AttributeError, e: ... print e ... Foo instance has no attribute 'b' 

If you want to get a list of attributes available for an object, try dir() or 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 

You can even call help() on dir (why, as an exercise for the reader):

 >>> 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 attributes. for a class object: its attributes, and recursively the attributes of its bases. for any other object: its attributes, its class attributes, and recursively the attributes of its class base classes. 

Otherwise, you can always see the code, unless you have been provided with any pre-compiled module by a third-party manufacturer, in which case you should require better documentation from your supplier (for example, some unit tests!)!

+2
source share

Usually an AttributeError contains some information about this:

 #!/usr/bin/env python class SomeClass(object): pass if __name__ == '__main__': sc = SomeClass() print sc.fail # Traceback (most recent call last): # File "4572362.py", line 8, in <module> # print sc.fail # AttributeError: 'SomeClass' object has no attribute 'fail' 
0
source share

All Articles