Name of type of object

J

Jive Dadson

The traceback routine prints out stuff like,

NameError: global name 'foo' is not defined

NameError is a standard exception type.

What if I want to print out something like that?
I've determined that "global name 'foo' is not defined" comes
from the __str__ member of the exception object.
Where does it find the string "NameError"? In general, if I have
an object, is there a way to obtain the name of the type of the object?

Thankee.
 
R

Randall Smith

Jive said:
The traceback routine prints out stuff like,

NameError: global name 'foo' is not defined

NameError is a standard exception type.

What if I want to print out something like that?
I've determined that "global name 'foo' is not defined" comes
from the __str__ member of the exception object.
Where does it find the string "NameError"? In general, if I have
an object, is there a way to obtain the name of the type of the object?

Thankee.

type(object) ?

Randall
 
S

Steven Bethard

Randall said:
type(object) ?

Doesn't work for old-style classes like exceptions:

py> e = NameError("global name 'foo' is not defined")
py> type(e)
<type 'instance'>
py> type(e).__name__
'instance'

For old-style classes, you'll need to go through __class__

py> e.__class__
<class exceptions.NameError at 0x00934900>
py> e.__class__.__name__
'NameError'

STeVe
 
G

George Sakkis

Steven Bethard said:
Doesn't work for old-style classes like exceptions:

py> e = NameError("global name 'foo' is not defined")
py> type(e)
<type 'instance'>
py> type(e).__name__
'instance'

For old-style classes, you'll need to go through __class__

py> e.__class__
<class exceptions.NameError at 0x00934900>
py> e.__class__.__name__
'NameError'

STeVe

To sum up:

def typename(obj):
try:
return obj.__class__.__name__
except AttributeError:
return type(obj).__name__


George
 
J

Jive Dadson

I don't think I've quite got it.

The application I'm writing has some similarities to an interactive
shell. Like an interactive shell, it executes arbitrary code that it
receives from an input stream. When it gets an exception, it should
create an informative message, regardless of the type of exception. The
traceback routine does that, somehow, some way, but I've tried to read
that code and figure out how and I don't get it.

The best I have so far is,

class Exec_thread(BG_thread):
""" Execute a line of code in the global namespace. """
def _process(s):
""" Process one instruction """
try:
exec s.product in globals()
except (Exception), e:
handle_error( typename(e)+ ": " + str(e) )


But that works only if the exception happens to be derived from
Exception. How do I handle the
general case?
 
J

Jeremy Bowers

But that works only if the exception happens to be derived from Exception.
How do I handle the
general case?

I believe the answer is that Exceptions not derived from Exception
shouldn't be thrown; it's basically a deprecated feature and has been for
a long time. I've never seen it happen. Add another "except" clause and
use it to bitch at the user for throwing something that isn't an Exception
:) You won't find any other code that throws anything but an exception,
unless either you or your user wrote it.
 
K

Kent Johnson

Jive said:
I don't think I've quite got it.

The application I'm writing has some similarities to an interactive
shell. Like an interactive shell, it executes arbitrary code that it
receives from an input stream. When it gets an exception, it should
create an informative message, regardless of the type of exception. The
traceback routine does that, somehow, some way, but I've tried to read
that code and figure out how and I don't get it.

The best I have so far is,

class Exec_thread(BG_thread):
""" Execute a line of code in the global namespace. """
def _process(s):
""" Process one instruction """
try:
exec s.product in globals()
except (Exception), e:
handle_error( typename(e)+ ": " + str(e) )

Have you looked at the traceback module? If you want to print the same kind of trace you get from
Python, just use traceback.print_exc().

import traceback
try:
# whatever
except:
traceback.print_exc()

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,731
Latest member
unutbu

Latest Threads

Top