473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Name of type of object

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.
Jul 18 '05 #1
6 1997
Jive Dadson wrote:
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
Jul 18 '05 #2
Randall Smith wrote:
Jive Dadson wrote:
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) ?


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
Jul 18 '05 #3

"Steven Bethard" <st************@gmail.com> wrote in message
news:J8********************@comcast.com...
Randall Smith wrote:
Jive Dadson wrote:
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) ?


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
Jul 18 '05 #4

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?
Jul 18 '05 #5
On Wed, 09 Feb 2005 21:57:15 +0000, Jive Dadson wrote:
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.
Jul 18 '05 #6
Jive Dadson wrote:
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


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

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ahmet AKGUN | last post by:
Hi; is it possible to open one form in .net platform that we have its name in string ? I have string sFormName = "frmCustomer"; and I must automatically open Customer form. or is it...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
11
by: Alexander Walker | last post by:
Hello I would like to write a method that allows me to pass a reference to an instance of a class, the name of a property of that class and a value to set that property to, the method would then...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
9
by: MikeB | last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project for school and one of the requirements is that every screen should have a "Help" button. I could do it by writing a clumsy...
7
by: Nemisis | last post by:
Hi everyone, Can anyone tell me if it is possible to pass in a property of an object into a sub, and within that sub, find out the name of the item that was passed along with the property name??...
6
by: William | last post by:
for example, I have a global object: extern Object myobj; Can gcc get this object by using string "myobj" at runtime?? I know C++ rtti doesnt support this, but I dont know if gcc can , ...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.