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
Post a Comment