471,123 Members | 849 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Getting a class name from within main

Hi,

Lets say I have the following class -

class MyClass:
def __init__(self):
print (__name__.split("."))[-1]

if __name__ == '__main__':
MyClassName = "MyClass"

I can print the name of the class from within the class scope as seen
above in the init, but is there any way of printing it from within the
main without creating an object of the MyClass type. I need to assign
the name of the class within my script, to a variable in main.

Thanks,

Barry.

Feb 7 '07 #1
3 1218
bg***@yahoo.com wrote:
Hi,

Lets say I have the following class -

class MyClass:
def __init__(self):
print (__name__.split("."))[-1]

if __name__ == '__main__':
MyClassName = "MyClass"

I can print the name of the class from within the class scope as seen
above in the init, but is there any way of printing it from within the
main without creating an object of the MyClass type. I need to assign
the name of the class within my script, to a variable in main.

Thanks,

Barry.
>>class A:
.... pass
....
>>print A.__name__
A
>>>
--
Robin Becker

Feb 7 '07 #2
In <11*********************@s48g2000cws.googlegroups. com>, bg_ie wrote:
class MyClass:
def __init__(self):
print (__name__.split("."))[-1]

if __name__ == '__main__':
MyClassName = "MyClass"

I can print the name of the class from within the class scope as seen
above in the init, but is there any way of printing it from within the
main without creating an object of the MyClass type. I need to assign
the name of the class within my script, to a variable in main.
Yes::

print MyClass.__name__

Ciao,
Marc 'BlackJack' Rintsch
Feb 7 '07 #3
On Wed, Feb 07, 2007 at 01:02:39AM -0800, bg***@yahoo.com wrote:
Hi,

Lets say I have the following class -

class MyClass:
def __init__(self):
print (__name__.split("."))[-1]
I would spell this:
print self.__class__.__name__
>
if __name__ == '__main__':
MyClassName = "MyClass"

I can print the name of the class from within the class scope as seen
above in the init, but is there any way of printing it from within the
main without creating an object of the MyClass type. I need to assign
the name of the class within my script, to a variable in main.

Thanks,

Barry.

--
http://mail.python.org/mailman/listinfo/python-list
Feb 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jacek Dziedzic | last post: by
7 posts views Thread by Thomas Matthews | last post: by
12 posts views Thread by Sunny | last post: by
5 posts views Thread by Xarky | last post: by
reply views Thread by Daniel Sélen Secches | last post: by
3 posts views Thread by johnny | last post: by
2 posts views Thread by karinmorena | last post: by

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.