Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there someway I can get specific details about an AttributeError exception in Python?

I'm trying to call a function. One of the parameters is a variable with attributes (which I know because of the AttributeError exception I got). I don't know the exact attributes this variable is supposed to have, so I was wondering if there was some way I can see some extra details about the exception, for example, which attribute it couldn't find. Thanks.

like image 777
Krzysztof Czelusniak Avatar asked Dec 31 '10 21:12

Krzysztof Czelusniak


2 Answers

AttributeError typically identifies the missing attribute. e.g.:

class Foo:
    def __init__(self):
        self.a = 1

f = Foo()
print(f.a)
print(f.b)

When I run that, I see:

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

That's pretty explicit. If you're not seeing something like that, please post the exact error you're seeing.

EDIT

If you need to force the printing of an exception (for whatever reason), you can do this:

import traceback

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

That should give you the full error message and traceback associated with the exception.

like image 140
Brian Clapper Avatar answered Oct 23 '22 05:10

Brian Clapper


The traceback should alert you to the attribute access that raised the AttributeError exception:

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

Alternatively, convert the Exception to str:

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

If you want to get a list of the attributes available on 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 is left 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'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.

Failing these... you could always look at the code, unless you've been provided some precompiled module by a third-party, in which case you should demand better documentation (say some unit tests!) from your supplier!

like image 44
Johnsyweb Avatar answered Oct 23 '22 03:10

Johnsyweb