Accessing base class variable [duplicate]

谁都会走 提交于 2019-12-25 04:08:43

问题


How do I access id from my child class?

class BaseClass:
    id = 'testing'

class MyClass(BaseClass):
    something = id

More specifically, I can't access id from class:User:

class BaseModel:
    id = db.Column(db.Integer, primary_key=True)

class User(db.Model, BaseModel):
    username = db.Column(db.String(35), nullable=False, unique=True)

    followed = db.relationship(
        'User',
        secondary=followers,
        primaryjoin=(followers.c.follower_id == id),
        secondaryjoin=(followers.c.followed_id == id),
        backref=db.backref('followers', lazy='dynamic'),
        lazy='dynamic')

回答1:


Here is a little program to test inheritance in your sample example:

class BaseClass:
    id = 'testing'

class MyClass(BaseClass):
    something = BaseClass.id

if __name__ == '__main__':
    print(MyClass().something) # >> "testing"

You should be able to replace id with BaseModel.id. If not, please report what error you're getting.



来源:https://stackoverflow.com/questions/52411968/accessing-base-class-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!