473,500 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More elegant to get a name: o.__class__.__name__

alf
Hi,
is there a more elegant way to get o.__class__.__name__. For instance I
would imagine name(o).

--
alf
Nov 30 '06 #1
4 3160

alf wrote:
Hi,
is there a more elegant way to get o.__class__.__name__. For instance I
would imagine name(o).
def name_of_type(o):
return o.__class__.__name__

name_of_type(o)
Carl Banks

P.S. name(o) suggests it's the name of the object, not the type
P.P.S. you could use type(o).__name__ but it doesn't work for old-style
classes

Nov 30 '06 #2
"alf" <ask@mewrote in message
news:z7******************************@comcast.com. ..
Hi,
is there a more elegant way to get o.__class__.__name__. For instance I
would imagine name(o).

--
alf
>>name = lambda o : o.__class__.__name__
name(1)
'int'

Go for it!

-- Paul
Nov 30 '06 #3
Carl Banks wrote:
alf wrote:
Hi,
is there a more elegant way to get o.__class__.__name__. For instance I
would imagine name(o).

def name_of_type(o):
return o.__class__.__name__

name_of_type(o)
Carl Banks

P.S. name(o) suggests it's the name of the object, not the type
P.P.S. you could use type(o).__name__ but it doesn't work for old-style
classes
You mean it doesn't work for old-style instances; OTOH __class__
doesn't work for old style classes:
>>class X: pass
....
>>X.__class__
AttributeError: class X has no attribute '__class__'

So to handle all cases, you'd have to go with:

def typename(o):
try: cls = o.__class__
except AttributeError: cls = type(o)
return cls.__name__
# or for fully qualified names
# return '%s.%s' % (cls.__module__, cls.__name__)

George

Dec 1 '06 #4
alf
alf wrote:
Hi,
is there a more elegant way to get o.__class__.__name__. For instance I
would imagine name(o).
decided to stick to o.__class__.__name__ for readibility.

--
alfz1
Dec 9 '06 #5

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

Similar topics

4
4802
by: Graeme Matthew | last post by:
Hi all, quick one, I hope I am explaining this properly, I am wanting to do some introspection on custom object instances, for example: import md5 >>> m = md5.new() >>> type(m) <type...
6
2002
by: Jive Dadson | last post by:
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...
161
7679
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
4
2662
by: *binarystar* | last post by:
Hello there, what method would you use to return the name of the class and/or instance introspectively eg. class Bollocks: def __init__( self ): print...
19
420
by: Kirk Strauser | last post by:
Given a class: how can I find its name, such as: 'foo' I'm writing a trace() decorator for the sake of practice, and am trying to print the name of the class that a traced method belongs...
6
2002
by: Adam Atlas | last post by:
Is it possible for an object, in its __init__ method, to find out if it is being assigned to a variable, and if so, what that variable's name is? I can think of some potentially ugly ways of...
3
1292
by: bg_ie | last post by:
Hi, Lets say I have the following class - class MyClass: def __init__(self): print (__name__.split(".")) if __name__ == '__main__': MyClassName = "MyClass"
14
2782
by: Harlin Seritt | last post by:
Hi, How does one get the name of a class from within the class code? I tried something like this as a guess: self.__name__ Obviously it didn't work. Anyone know how to do that?
1
13004
by: Davy | last post by:
Hi all, How to get the class name of an object from an introspect way? For example, Class cls:pass obj = cls() I want to get function(obj) = 'cls'
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.