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

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.