[Solved]-How to access the meta attributes of a superclass in Python?

14πŸ‘

βœ…

In your example it seems that you are trying to override the attribute of the meta of the super class. Why not use meta inheritance?

class MyCustomAuthentication(Authentication):
    pass

class SpecializedResource(ModelResource):
    class Meta:
        authentication = MyCustomAuthentication()

class TestResource(SpecializedResource):
    class Meta(SpecializedResource.Meta):
        # just inheriting from parent meta
        pass
    print Meta.authentication

Output:

<__main__.MyCustomAuthentication object at 0x6160d10> 

so that the TestResourceβ€˜s meta are inheriting from parent meta (here the authentication attribute).

Finally answering the question:

If you really want to access it (for example to append stuff to a parent list and so on), you can use your example:

class TestResource(SpecializedResource):
    class Meta(SpecializedResource.Meta):
        authentication = SpecializedResource.Meta.authentication # works (but hardcoding)

or without hard coding the super class:

class TestResource(SpecializedResource):
    class Meta(SpecializedResource.Meta):
        authentication = TestResource.Meta.authentication # works (because of the inheritance)
πŸ‘€astreal

Leave a comment