decorator - How to add attribute to python *class* that is _not_ inherited? -


i need able set flag on class (not on instance of class) not visible subclass. question is, possible, , how if is?

to illustrate, want this:

class master(someotherclass):     __flag__ = true  class child(master):     pass 

... hasattr(master, "__flag__") should return true master false child. possible? if so, how? don't want have explicitly set __flag__ false in every child.

my initial thought define __metaclass__, don't have luxury of doing because master inherits other classes , metaclasses don't control , private.

ultimately i'm wanting write decorator can like:

@hide_this class master(someotherclass): pass  @hide_this class child(master): pass  class grandchild(child): pass ... cls in (master, child, grandchild)     if cls.__hidden__:         # master, child     else:         # grandchild 

you close:

class master(someotherclass):     __flag = true  class child(master):     pass 

two leading underscores without trailing underscores invokes name mangling, attribute named _master__flag. therefore if check:

hasattr(cls, '_{}__flag'.format(cls.__name__)) 

it only true master, not child.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -