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

发布于 2024-12-04 18:06:51 字数 499 浏览 3 评论 0原文

我有一个 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

我想减少访问参数所需的代码。我应该在 some_function 顶部放置什么来仅通过参数名称(param_1、param_2、param_3)访问参数?我应该在 MyClass 中的某个位置放置什么来将此快捷方式应用于它的所有方法,而不仅仅是 some_function ?

I have a Python class, which contains several nested parameter groups:

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)? And what should I place somewhere in MyClass to apply this shortcut for all its methods, not only some_function?

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

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

发布评论

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

评论(3

魔法唧唧 2024-12-11 18:06:51

我会以 开始该函数

spg = self.big_parameter_group.small_parameter_group

,然后使用该缩写。我想,如果您想在前面带有 self 的任何地方使用它们,您可以在 __init__() 中定义这样的缩写。

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.

清风不识月 2024-12-11 18:06:51

一种方法是为每个属性创建属性:

@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

另一种更强大但不太明确的方法是重写 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)

这适用于与正则表达式指定的格式匹配的任何属性( param_[some number])

这两种方法都允许您调用 self.param_1 等,但它只是用于检索。如果你想设置属性,你还需要创建一个setter:

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

或者补充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)

还没有测试这些,所以可能会有拼写错误,但这个概念应该可行)

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:

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])

Both of these methods will allow you to call self.param_1 etc, but it's just for retriving. If you want to set the attributes you'll need to also create a 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:

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)

岁月静好 2024-12-11 18:06:51

在你的 init 内部你总是可以这样做。

self.local_param_1 = self.big_parameter_group.small_parameter_group.param_1

Inside your init you could always do.

self.local_param_1 = self.big_parameter_group.small_parameter_group.param_1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文