ValueError:可变默认值< class' dict'>对于场标头,不允许:使用Default_factory

发布于 2025-02-07 10:22:17 字数 894 浏览 1 评论 0原文

我正在尝试使用新的Python的功能(Dataclasses)。我正在尝试初始化变量,并且会遇到错误:

raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'dict'> for field headers is not allowed: use default_factory

我的代码:

@dataclass
class Application():
    __config = ConfigParser()
    __config.read('mydb.ini')
    __host: str = __config.get('db','dbhost')
    __user: str = __config.get('db','dbuser')
    __password: str = __config.get('db','dbpw')
    __database: str = __config.get('db','database')
    url: str = "https://xxxx.domain.com/"
    headers: str = {'X-ApiKeys':'accessKey=xxxxxxx;secretKey=xxxxx','Content-Type': 'application/json'}



def main(self):
    print(self.__host,self.__user,self.__password, self.__database)
   


app = Application()
if __name__=="__main__":
    app.main()

初始化字典的正确方法是什么?

I am trying to get used with new python's features (dataclasses). I am trying to initialize variables and I get error:

raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'dict'> for field headers is not allowed: use default_factory

My code:

@dataclass
class Application():
    __config = ConfigParser()
    __config.read('mydb.ini')
    __host: str = __config.get('db','dbhost')
    __user: str = __config.get('db','dbuser')
    __password: str = __config.get('db','dbpw')
    __database: str = __config.get('db','database')
    url: str = "https://xxxx.domain.com/"
    headers: str = {'X-ApiKeys':'accessKey=xxxxxxx;secretKey=xxxxx','Content-Type': 'application/json'}



def main(self):
    print(self.__host,self.__user,self.__password, self.__database)
   


app = Application()
if __name__=="__main__":
    app.main()

What's the proper way to initialize dictionaries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2025-02-14 10:22:17

数据级有一些有用的东西来定义复杂字段。您需要的一个称为 field 。这个有参数default_factory需要接收可召唤,并且在某些地方lambda可以进行救援。因此,使用上面的代码看起来像(仅与DICE的一部分):

from dataclasses import field
from typing import Dict

@dataclass
class Application():
    ...
    headers: Dict[str, str] = field(
        default_factory=lambda: {'X-ApiKeys':'accessKey=xxxxxxx;secretKey=xxxxx','Content-Type': 'application/json'}
    )

Dataclasses have a few useful things for defining complex fields. The one that you need is called field. This one has the argument default_factory that needs to receive callable, and there is where lambda comes to the rescue. So using this above, code that will work looks like (just part with dict):

from dataclasses import field
from typing import Dict

@dataclass
class Application():
    ...
    headers: Dict[str, str] = field(
        default_factory=lambda: {'X-ApiKeys':'accessKey=xxxxxxx;secretKey=xxxxx','Content-Type': 'application/json'}
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文