AlGiorgio AlGiorgio - 7 months ago 10
Python Question

Modifying ancestor nested Meta class in descendant

Suppose I have:

class A(object):

class Meta:
a = "a parameter"


class B(A):

class Meta:
a = "a parameter"
b = "b parameter"


How can I avoid having to rewrite the whole Meta class, when I only want to append
b = b parameter
to it?

Answer

You could subclass A.Meta:

class B(A):    
    class Meta(A.Meta):
        b = "b parameter"

Now B.Meta inherits all attributes from A.Meta, and all you have to do is declare overrides or new attributes.

Comments