Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从变量中设置类(自身)变量?_Python_Variables - Fatal编程技术网

Python 从变量中设置类(自身)变量?

Python 从变量中设置类(自身)变量?,python,variables,Python,Variables,这是糟糕的编程实践,是的,我知道,但这些脚本完全是我的,这种技术将大大简化我的编码 现在,我有一个SQLite数据库,其中包含一组表示脚本配置指令的键值对。脚本本身是一个类,我将它导入到其他脚本中 所以,现在,当我想要访问配置变量时,我调用如下内容: myLib.myClass.getConfigVariable("theItemIWant") 在脚本中使用配置变量时,这会变得非常糟糕 所以我想简化对这些变量的访问。我可以使用一个字典,它在类加载时预先填充并执行以下操作: myLib.myCl

这是糟糕的编程实践,是的,我知道,但这些脚本完全是我的,这种技术将大大简化我的编码

现在,我有一个SQLite数据库,其中包含一组表示脚本配置指令的键值对。脚本本身是一个类,我将它导入到其他脚本中

所以,现在,当我想要访问配置变量时,我调用如下内容:

myLib.myClass.getConfigVariable("theItemIWant")
在脚本中使用配置变量时,这会变得非常糟糕

所以我想简化对这些变量的访问。我可以使用一个字典,它在类加载时预先填充并执行以下操作:

myLib.myClass.config['theItemIWant']
但我想得更优雅一点。我编写了一个单独的配置类,我想提供对配置项的变量级访问

所以我想做的是:

myLib.Config().theItemIWant
或者在脚本中实例化一个对象,如下所示:

def myRoutine(self):
    cfg = myLib.Config()
    print cfg.theItemIWant
我读过一些关于丑陋的(使用exec)方法来实现这一点的文章,实际上我对此没有意见,但我不知道如何以这种方式设置类级变量。大多数人建议要么使用exec,要么修改vars或globals,但我不确定这是否能直接在Config类上完成变量设置,而不是在其他地方

使用exec失败:

SyntaxError: unqualified exec is not allowed in function '__init__' it contains a nested function with free variables

因此,我看到的唯一方法是修改vars(),但我不确定这如何应用于类。

我认为您需要的只是分配给一个成员变量,如下所示:

class Foo(object):
      pass

cfg = Foo()
cfg.item_i_want = "An item"
print cfg.item_i_want
这将打印“一个项目”。见:


如果要动态选择变量名,请使用
setattr(cfg,“另一项”、“另一项”)

您可以简单地为您的配置对象实现
\uuu getattr\uuu()
函数,如

def __getattr__(self, name):
    if name in self.items:
         return self.items[name]
    else:
         raise AttributeError()

有关python文档中
\uuu getattr\uuuu()
的说明,请参阅。

一种尝试不重新发明轮子的解决方案。当您只想读取配置一次,并且它的结构是扁平的时,它就可以工作

from collections import namedtuple

def getConfig(config_source):
  # read the config_source into a dict
  # config_source might be a file name or whatnot
  config_dict = read_somehow(config_source)
  tuple_class = namedtuple('Config', config_dict.keys())
  return tuple_class(**config_dict)
该函数返回一个不可变对象,其属性以配置参数名称命名

  # suppose config file is something like:
  # a = 1 
  # foo = bar

  cfg = getConfig(...)
  print cfg.a # prints 1
  print cfg.foo # prints foo
  print cfg.unknown # raises AttributeError

我曾经使用这种方法阅读标准
ConfigParser
实例中的部分。

不清楚您想做什么,或者为什么要使用
exec
,尤其是因为您没有显示完整的示例。如果您想设置成员变量,只需设置它。您可以使用
exec
给出代码示例吗?现在还不清楚你想要实现什么。为什么您不能直接执行
myLib.config.ittemiwant=which
?它的拼写可能重复
\uuuu getattr\uuuuu
,或者使用
setattr
操作符。这样您就无法从数据库条目中“动态”设置属性。。。