Getting derived class name statically in python -


I can apply to how to achieve stable method get_class_name () expected results.

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

  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    

instead of a 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   

Of course you do not need a function / method to do just could access the __ name __ class attribute directly to: < Pre> print sub_classA.__name__ # - & gt; Sub-class A # etc

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -