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

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -