加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python:将函数作为参数传递以初始化对象的方法.还是Pythonic?

发布时间:2020-12-17 17:40:39 所属栏目:Python 来源:网络整理
导读:我想知道是否存在将函数作为参数传递给对象的可接受方法(即在init块中定义该对象的方法). 更具体地说,如果函数取决于对象参数,该怎么做. 似乎足以将函数传递给对象的pythonic,函数就像其他任何对象一样是对象: def foo(a,b): return a*bclass FooBar(object

我想知道是否存在将函数作为参数传递给对象的可接受方法(即在init块中定义该对象的方法).

更具体地说,如果函数取决于对象参数,该怎么做.

似乎足以将函数传递给对象的pythonic,函数就像其他任何对象一样是对象:

def foo(a,b):
    return a*b

class FooBar(object):
    def __init__(self,func):
        self.func = func

foobar = FooBar(foo)
foobar.func(5,6)

# 30

因此,当您引入对对象其他属性的依赖关系时,问题就会立即出现.

def foo1(self,b):
    return self.a*b

class FooBar1(object):
    def __init__(self,func,a):
        self.a=a
        self.func=func

# Now,if you try the following:
foobar1 = FooBar1(foo1,4)
foobar1.func(3)
# You'll get the following error:
# TypeError: foo0() missing 1 required positional argument: 'b'

这可能只是违反了python中OOP的一些神圣原则,在这种情况下,我将不得不做其他事情,但似乎也可能有用.

尽管有几种可能的解决方法,但我想知道哪种(如果有)被认为是最可接受的.

解决方案1

foobar1.func(foobar1,3)

# 12
# seems ugly

解决方案2

class FooBar2(object):
    def __init__(self,a):
        self.a=a
        self.func = lambda x: func(self,x)

# Actually the same as the above but now the dirty inner-workings are hidden away. 
# This would not translate to functions with multiple arguments unless you do some ugly unpacking.
foobar2 = FooBar2(foo1,7)
foobar2.func(3)

# 21

任何想法,将不胜感激!

最佳答案
将函数传递给对象就可以了.该设计没有错.

但是,如果要将该函数转换为绑定方法,则必须小心一点.如果您执行诸如self.func = lambda x:func(self,x)之类的操作,则会创建一个引用循环-self引用了self.func,而存储在self.func中的lambda引用了self. Python的垃圾回收器确实会检测参考周期并最终对其进行清理,但这有时可能需要很长时间.过去,我的代码中曾有参考循环,这些程序经常使用500 MB以上的内存,因为python不会足够频繁地垃圾收集不需要的对象.

正确的解决方案是使用weakref模块创建对self的弱引用,例如:

import weakref

class WeakMethod:
    def __init__(self,instance):
        self.func = func
        self.instance_ref = weakref.ref(instance)

        self.__wrapped__ = func  # this makes things like `inspect.signature` work

    def __call__(self,*args,**kwargs):
        instance = self.instance_ref()
        return self.func(instance,**kwargs)

    def __repr__(self):
        cls_name = type(self).__name__
        return '{}({!r},{!r})'.format(cls_name,self.func,self.instance_ref())


class FooBar(object):
    def __init__(self,a):
        self.a = a
        self.func = WeakMethod(func,self)

f = FooBar(foo1,7)
print(f.func(3))  # 21

以下所有解决方案都会创建一个参考周期,因此很糟糕:

> self.func = MethodType(函数,自我)
> self.func = func .__ get __(self,type(self))
> self.func = functools.partial(func,self)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读