简体   繁体   English

Python-自动更新继承的类别中的基础类别属性(别名)

[英]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. 我敢肯定这是微不足道的,但是我似乎无法从Python(3)文档中找到任何见解。

Given the following code, how can I ensure that the t class attribute in the base class is updated in the inherited class. 给定以下代码,如何确保在继承的类中更新基类的t类属性。

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. 我现在也希望能够将Article.table称为Article.t

Thanks! 谢谢!

Make t a property : 使tproperty

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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