By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

AttributeError: How to list existing attributes?

P: n/a
Hi,

how can you list the attributes of an object if you catch an
AttributeError?

I couldn't find a reference in the exception object, which
points to the object.

I want to call dir() on the object to list the user the known
attributes.

Is there a way to find the object by inspecting the stacktrace?

Thomas

--
Thomas GŁttler, http://www.thomas-guettler.de/ http://www.tbz-pariv.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de

Sep 19 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Thomas Guettler wrote:
Hi,

how can you list the attributes of an object if you catch an
AttributeError?

I couldn't find a reference in the exception object, which
points to the object.

I want to call dir() on the object to list the user the known
attributes.

Is there a way to find the object by inspecting the stacktrace?
By looking at the code at the line the stacktrace lists? And at least for
me, there is a type-information as well:

Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
clWelcome to rlcompleter2 0.96
for nice experiences hit <tabmultiple times
>>class Foo(object): pass
....
>>f = Foo()
f.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'foo'
>>>
You can't possibly know which attributes the object has, though. Because it
might be an attribute dynamically added.

So the best thing is to put a print-statement before the exception-throwing
line or put a

import pdb; pdb.set_trace()

there, and fiddle around with the object.

Diez
Sep 19 '07 #2

P: n/a
Diez B. Roggisch wrote:
Thomas Guettler wrote:
>Hi,

how can you list the attributes of an object if you catch an
AttributeError?

I couldn't find a reference in the exception object, which
points to the object.

I want to call dir() on the object to list the user the known
attributes.

Is there a way to find the object by inspecting the stacktrace?

By looking at the code at the line the stacktrace lists? And at least for
me, there is a type-information as well:
Hello,

I want to customize the debug tracebacks displayed in django.
It is not to find one particular error. That's why changing the
source is not a solution.

Thomas

--
Thomas GŁttler, http://www.thomas-guettler.de/ http://www.tbz-pariv.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de

Sep 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.