gpt4 book ai didi

python:使用包含变量的名称访问实例变量

转载 作者:太空狗 更新时间:2023-10-29 22:04:22 25 4
gpt4 key购买 nike

在 python 中,我试图访问一个实例变量,我需要使用另一个变量的值来确定名称:示例实例变量:user.remote.directory 它指向 'servername:/mnt 的值/.....' 和 user 部分包含用户的 userid,例如 joe.remote.directory

在另一个类中,我需要能够使用包含用户 id joe 的变量访问 joe.remote.directory。我尝试了 variable.remote.directory 但它不起作用,有什么建议吗?

最佳答案

不确定您想要什么,但我认为 getattr(obj, 'name') 可能会有所帮助。参见 http://docs.python.org/library/functions.html#getattr

关于python:使用包含变量的名称访问实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com