the question straightforward , have tried make indentation proper readability. starting of python , here have now:
class baseklass(): .... class derivedklass1(baseklass): display_name = "derivedklass1" class derivedklass2(baseklass): display_name = "derivedklass2"
so derived classes has
display_name variable class name hardcoded.
what want have display_name set in baseklass , remove display_name declaration child classes follows:
class baseklass(): display_name = <some way set display name callee class name> class derivedklass1(baseklass): .... class derivedklass2(baseklass): ....
so derivedklass1.display_name should return "derivedklass1" , derivedklass2.display_name should return "derivedklass2".
i know missing obvious , expecting lot of rtfm comments regardless want learn how dynamically set python class name class level attribute. feel free down vote post if thankful if leave answer well.
thanks,
you can access object's class name with
obj.__class__.__name__
you'll need once instance has been created tho. straight forward way be
class myclass: def __init__(self): self.display_name = self.__class__.__name__
you use metaclass have set automatically, don't think it'd worth trouble, plus can't remember how work right now.
Comments
Post a Comment