setattr和getattr与方法 [英] setattr and getattr with methods

查看:151
本文介绍了setattr和getattr与方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个锅炉板类,它将一些动作委派给参考类.看起来像这样:

I have a boiler platey class that delegates some actions to a reference class. It looks like this:

class MyClass():

    def __init__(self, someClass):            
        self.refClass = someClass

    def action1(self):
        self.refClass.action1()

    def action2(self):
        self.refClass.action2()

    def action3(self):
        self.refClass.action3()

这是refClass:

This is the refClass:

class RefClass():

    def __init__(self):
        self.myClass = MyClass(self)

    def action1(self):
        #Stuff to execute action1

    def action2(self):
        #Stuff to execute action2

    def action3(self):
        #Stuff to execute action3

我想使用Python元编程来使它更加优雅和可读性,但是我不确定如何做到这一点.

I'd like to use Python Metaprogramming to make this more elegant and readable, but I'm not sure how.

我听说过 setattr

I've heard of setattr and getattr, and I think I could do something like

class MyClass():

    def __init__(self, someClass):            
        self.refClass = someClass

    for action in ['action1', 'action2', 'action3']:
        def _delegate(self):
            getattr(self.refClass, action)()

然后我知道我需要从某个地方进行此操作,

And then I know I need to do this from somewhere, I guess:

MyClass.setattr(action, delegate)

我只是无法完全理解这个概念.我了解不重复代码以及使用函数式编程使用for循环生成方法的基础知识,但是我不知道如何从其他地方调用此方法.嘿!

I just can't totally grasp this concept. I understand the basics about not repeating code, and generating the methods with a for loop with functional programming, but then I don't know how to call this methods from elsewhere. Heeeelp!

推荐答案

Python已包含对广义委托到所包含类的支持.只需将MyClass的定义更改为:

Python already includes support for generalized delegation to a contained class. Just change the definition of MyClass to:

class MyClass:

    def __init__(self, someClass):            
        self.refClass = someClass  # Note: You call this someClass, but it's actually some object, not some class in your example

    def __getattr__(self, name):
        return getattr(self.refClass, name)

定义后,在实例上调用 __getattr__ 只要在实例本身上找不到属性,就可以访问该属性.然后,通过调用 getattr 来委托包含对象,以在包含对象上查找属性对象并返回它.每次执行动态查找都会花费一些时间,因此,如果要避免使用动态查找,则可以在__getattr__首次请求属性时懒惰地缓存属性,因此可以直接进行后续访问:

When defined, __getattr__ is called on the instance with the name of the accessed attribute any time an attribute is not found on the instance itself. You then delegate to the contained object by calling getattr to look up the attribute on the contained object and return it. This costs a little each time to do the dynamic lookup, so if you want to avoid it, you can lazily cache attributes when they're first requested by __getattr__, so subsequent access is direct:

def __getattr__(self, name):
     attr = getattr(self.refClass, name)
     setattr(self, name, attr)
     return attr

这篇关于setattr和getattr与方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆