Getting derived class name statically in python -
I can apply to how to achieve stable method I need to get a Instaling without the name of the class (I know it can be done with an example) Python version 2.6 instead of a Of course you do not need a function / method to do just get_class_name () expected results.
class Base_class (): @staticmethoddef get_class_name ():. Return & lt; ?? & Gt; Class sub_classA (base_class): pass class sub_classB (base_class): pass print sub_classA.get_class_name () # 'sub_classA' should print sub_classB.get_class_name () # should print 'sub_classB' Print base_class.get_class_name () # 'base_class' should print
make classmethod :
class base_class (): @classmethod def get_class_name (CLS): return cls .__ name__ class sub_classA (base_class): pass class sub_classB (base_class): Print sub_classA.get_class_name (pass) # - & gt; Sub-class one print sub_classB.get_class_name () # - & gt; Sub_classB print base_class.get_class_name () # - & gt; base_class
could access the __ name __ class attribute directly to: < Pre>
print sub_classA.__name__ # - & gt; Sub-class A # etc
Comments
Post a Comment