472,144 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

AttributeError: How to list existing attributes?

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
2 1933
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
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.

Similar topics

reply views Thread by Erlend Fuglum | last post: by
17 posts views Thread by Pierre Fortin | last post: by
2 posts views Thread by Eric Lemmon | last post: by
reply views Thread by PlayDough | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.