python - Set attribute of subclass in method of super class -


i have following scenario:

class baseclass(object):     extra_fields = []     @classmethod     def extend(cls, key):         cls.extra_fields.append(key)  class a(baseclass):     pass  class b(baseclass):     pass  a.extend("foo") 

now, extend of baseclass called, setting baseclass.extra_fields ["foo"]. then, a.extra_fields ["foo"], b.extra_fields.

is there way in extend modify subclass on invoked (without defining extend on subclasses, may not known in advance)?

class baseclass(object):         @classmethod     def extend(cls, key):         if not 'extra_fields' in cls.__dict__:             cls.extra_fields=[]         cls.extra_fields.append(key)     class a(baseclass):     pass  class b(baseclass):     pass  a.extend("foo") 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -

r - Update two sets of radiobuttons reactively - shiny -