Getting a class name from within main

B

bg_ie

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.
 
M

Marc 'BlackJack' Rintsch

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
 
R

Robin Becker

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.
.... pass
....
 
C

Chris Lambacher

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__
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top