【问题标题】:Python class variable not updatingPython类变量不更新
【发布时间】:2013-05-09 14:10:04
【问题描述】:

我有一个类正在接受一个 Id 并尝试更新变量 current_account,但是当我打印出 current_account 的详细信息时它没有更新。

有人对此有任何想法吗? python 新手,所以可能会做一些我看不到的愚蠢的事情。

class UserData:
    def __init__(self, db_conn=None):
        if None == db_conn:
            raise Exception("DB Connection Required.")

        self.db = db_conn
        self.set_my_account()
        self.set_accounts()
        self.set_current_account()

    def set_current_account(self, account_id=None):
        print account_id
        if None == account_id:
            self.current_account = self.my_account
        else:
            if len(self.accounts) > 0:
                for account in self.accounts:
                    if account['_id'] == account_id:
                        self.current_account = account
                        print self.current_account['_id']
            else:
                raise Exception("No accounts available.")

假设set_my_account() 得到一个帐户数据字典,而set_accounts() 得到一个帐户数据字典列表。

所以当我执行以下操作时:

user_data = UserData(db_conn=db_conn)
user_data.set_current_account(account_id=account_id)

其中db_conn 是有效的数据库连接,account_id 是有效的帐户 ID。

我从以上两行中得到以下内容。

None
518a310356c02c0756764b4e
512754cfc1f3d16c25c350b7

所以None 值来自类的声明,接下来的两个值来自对set_current_account() 的调用。第一个 id 值是我要设置的值。第二个id 值是已经从类__init__() 方法中设置的值。

【问题讨论】:

  • 请注意,None == account_id 几乎不是惯用的 Python。 None 是一个单例对象,使用if account_id is None: 对其进行测试。
  • 作为记录,您的问题是关于 实例变量 而不是类变量,并且在定义自己的类时,您应该始终从 object 继承,如下所示: class UserData(object):
  • 感谢您的信息,将按照建议更新课程。
  • 正题,我无法复制你的行为。当我运行您的代码时(通过将 my_accountaccounts 设置为初始化程序中的简单字典和字典列表进行修改),它按预期工作。您还有什么可以告诉我们如何使用代码来帮助重现问题的吗?您能否提供一个完整的最小示例来显示不正确的行为?
  • 我看看我能做什么。

标签: python class variables python-2.7


【解决方案1】:

有很多冗余和非 Pythonic 结构。我清理了代码以帮助我了解您要做什么。

class UserData(object):
    def __init__(self, db_conn):
        self.db = db_conn
        self.set_my_account()
        self.set_accounts()
        self.set_current_account()

    def set_current_account(self, account_id=None):
        print account_id
        if account_id is None:
            self.current_account = self.my_account
        else:
            if not self.accounts:
                raise Exception("No accounts available.")

            for account in self.accounts:
                if account['_id'] == account_id:
                   self.current_account = account
                   print self.current_account['_id']

user_data = UserData(db_conn)
user_data.set_current_account(account_id)

当没有显式参数的调用无效时,您使用了默认参数(db_conn=None)。是的,您现在可以拨打__init__(None),但您也可以拨打__init__('Nalum');你不能保护一切。

通过移动“无帐户”异常,块快速失败,您可以节省一级缩进。

调用 UserData(db_conn=db_conn) 有效但不必要地重复。

不幸的是,我仍然无法弄清楚您要完成什么,这可能是最大的缺陷。变量名对于帮助读者(可能是未来的你)理解代码非常重要。 current_accountmy_accountaccount_idcurrent_account['_id'] 的意图如此模糊,以至于您应该真正考虑更独特、信息丰富的名称。

【讨论】:

  • 感谢您的反馈,我已更新代码以反映信息。我来自 PHP 背景,所以我想我会将其中的一些内容带入我的代码中。我想做的是创建一个帐户切换器。所以my_account 是登录的用户帐户,current_account 是系统读取的内容,因此用户可以切换帐户。也许有更好的方法来做到这一点。 account_id 实际上是我们要切换到的帐户的 ID。
【解决方案2】:

弄清楚它是什么。

数据正在代码库中的其他位置进行更改。它现在按预期工作。

感谢大家指出我做错的以 Python 为中心的事情,很高兴得到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2019-05-02
    • 2022-01-05
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多