Get name of class without instance

B

Bryan

Given a class:

class Foo(object):
pass

How can I get the name "Foo" without having an instance of the class?

str(Foo) gives me more than just the name Foo. "__main__.Account"
Foo.__class__.__name__ gives me "type"

I don't want to do:
Foo().__class__.__name__ if possible. I would rather avoid the
constructor. I just want to get a string "Foo"
 
J

Jean-Michel Pichavant

Bryan said:
Given a class:

class Foo(object):
pass

How can I get the name "Foo" without having an instance of the class?

str(Foo) gives me more than just the name Foo. "__main__.Account"
Foo.__class__.__name__ gives me "type"

I don't want to do:
Foo().__class__.__name__ if possible. I would rather avoid the
constructor. I just want to get a string "Foo"
'Foo'

:cool:

JM
 
T

Tim Golden

Bryan said:
Given a class:

class Foo(object):
pass

How can I get the name "Foo" without having an instance of the class?

str(Foo) gives me more than just the name Foo. "__main__.Account"
Foo.__class__.__name__ gives me "type"

I don't want to do:
Foo().__class__.__name__ if possible. I would rather avoid the
constructor. I just want to get a string "Foo"

You are going to kick yourself:

class Foo (object): pass

print Foo.__name__


TJG
 
B

Bryan

You are going to kick yourself:

class Foo (object): pass

print Foo.__name__

TJG

Whoops.

How come dir(Foo) does not show __name__? That is how I was
investigating the possibilities.
 
J

J. Cliff Dyer

Given a class:

class Foo(object):
pass

How can I get the name "Foo" without having an instance of the class?

str(Foo) gives me more than just the name Foo. "__main__.Account"
Foo.__class__.__name__ gives me "type"

I don't want to do:
Foo().__class__.__name__ if possible. I would rather avoid the
constructor. I just want to get a string "Foo"

I'll give you a hint:
True
 
D

David Stanek

Try Foo.__name__ if Foo is a class object.

Given a class:

class Foo(object):
pass

How can I get the name "Foo" without having an instance of the class?

str(Foo) gives me more than just the name Foo. "__main__.Account"
Foo.__class__.__name__ gives me "type"

I don't want to do:
Foo().__class__.__name__ if possible. I would rather avoid the
constructor. I just want to get a string "Foo"

--
Sent from my mobile device

David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek
 
T

Terry Reedy

Bryan said:
How come dir(Foo) does not show __name__? That is how I was
investigating the possibilities.

Interesting question. Seems like an oversight. dir(some_module) and
dir(some_function) include '__name__'. I suggest you search the tracker
for existing issues on this subject and add a feature request if there
is none.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top