简体   繁体   中英

Python - Automatically update base class attribute (alias) in inherited class

I'm sure this is something trivial, but I can't seem to find any insight from the Python (3) docs.

Given the following code, how can I ensure that the t class attribute in the base class is updated in the inherited class.

class DbBase:
    table = None
    t = table

class Articles(DbBase):
    table = Table(....)

I'd now like to be able to refer to Article.table as Article.t as well.

Thanks!

Make t a property :

class DbBase:
    table = None
    @property
    def t(self):
        return self.table

class Articles(DbBase):
    table = {} # for demo purposes

A = Articles()
A.t # returns {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM