Understanding Class Type '__main__.classname'
Solution 1:
As you have not defined a
__repr__(or__str__) on the class, it's inherited the one from the superclass --objectand that how its written there. So, all your class instances are expressed that way. As for the class itself, you need to change the__repr__/__str__on the metaclass i.e. the class of which our class in question is an instance of; the default metaclass istype.__main__is the name of the module, here as you are directly executing it, its being considered as a script and all scripts have the name__main__in PythonThere's a
.in between becauseFractionis an attribute of the script__main__, the module; and belongs to the module level scope
Example:
In [47]: classMyMeta(type):
...: def__repr__(cls):
...: return'Whatever...'
...:
In [48]: classMyClass(metaclass=MyMeta):
...: def__repr__(self):
...: return'Howdy...'
...:
In [49]: obj = MyClass()
In [50]: print(obj)
Howdy...
In [51]: print(type(obj))
Whatever...
For Python2, you need to define __metaclass__ as a class attribute.
Solution 2:
The name of the script being run is always __main__. This is why you check for this specific name, and why classes defined in the script are attributes of it.
Post a Comment for "Understanding Class Type '__main__.classname'"