简体   繁体   English

Python:如何将某个对象的命名空间导入当前命名空间?

[英]Python: how can I import the namespace of some object to current namespace?

I have a Python class, which contains several nested parameter groups: 我有一个Python类,它包含几个嵌套的参数组:

class MyClass(object):
    #some code to set parameters

    def some_function(self):
        print self.big_parameter_group.small_parameter_group.param_1
        print self.big_parameter_group.small_parameter_group.param_2
        print self.big_parameter_group.small_parameter_group.param_3

I want to reduce code needed to access parameters. 我想减少访问参数所需的代码。 What should I place at the top of some_function to access the parameters simply by their names (param_1, param_2, param_3)? 我应该在some_function的顶部放置什么来简单地通过名称访问参数(param_1,param_2,param_3)? And what should I place somewhere in MyClass to apply this shortcut for all its methods, not only some_function? 我应该在MyClass中放置什么来为其所有方法应用此快捷方式,而不仅仅是some_function?

I would start the function with 我会用它开始这个功能

spg = self.big_parameter_group.small_parameter_group

and then use that abbreviation. 然后使用该缩写。 You could define abbreviations like this in __init__() , I suppose, if you wanted to use them everywhere with self on the front. 你可以这样定义的缩写__init__()我想,如果你想用在任何地方使用他们self的前面。

One way is to create properties for each of them: 一种方法是为每个属性创建属性:

@property
def param_1(self):
    return self.big_parameter_group.small_parameter_group.param_1
@property
def param_2(self):
    return self.big_parameter_group.small_parameter_group.param_2
@property
def param_2(self):
    return self.big_parameter_group.small_parameter_group.param_2

Another more robust but less explicit way would be to override the getattr method like so: 另一个更健壮但不太明确的方法是覆盖getattr方法,如下所示:

def __getattr__(self, name):
    import re
    p = re.compile('param_[0-9]+')
    if p.match(name):
        return getattr(self.big_parameter_group.small_parameter_group, name)
    else:
        return super(MyClass, self).__getattr__(name)

This will work for any property that matches the format specified by the regex ( param_ [some number]) 这适用于与正则表达式指定的格式匹配的任何属性( param_ [some number])

Both of these methods will allow you to call self.param_1 etc, but it's just for retriving. 这两种方法都允许你调用self.param_1等,但它只是用于重新审阅。 If you want to set the attributes you'll need to also create a setter: 如果要设置属性,还需要创建一个setter:

@param_1.setter
    def param_1(self, value): 
        print 'called setter'
        self.big_parameter_group.small_parameter_group.param_1 = value

Or to complement getattr : 或者补充getattr

def __setattr__(self, name, value):
    import re
    p = re.compile('param_[0-9]+')
    if p.match(name):
        return setattr(self.big_parameter_group.small_parameter_group, name, value)
    else:
        return super(MyClass, self).__setattr__(name, value)

(Haven't tested these out so there may be typos but the concept should work) (没有测试过这些,所以可能有拼写错误,但概念应该有效)

在您的初始化中,您可以随时做到。

self.local_param_1 = self.big_parameter_group.small_parameter_group.param_1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM