访问基类变量 [英] Accessing base class variable

查看:79
本文介绍了访问基类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从子班级访问id?

class BaseClass:
    id = 'testing'

class MyClass(BaseClass):
    something = id

更具体地说,我无法从class:User访问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')

推荐答案

在您的示例示例中,有一个用于测试继承的小程序:

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"

您应该可以将id替换为BaseModel.id.如果没有,请报告您遇到的错误.

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

这篇关于访问基类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆