__call__被认为是有害的还是必不可少的? [英] __call__ considered harmful or indispensable?

查看:74
本文介绍了__call__被认为是有害的还是必不可少的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我个人不会在我的课程中使用__call__方法,但是我已经偶然遇到了
,在这里工作的其他代码写的代码

人。有一天,我用一个更明显的方法名称替换了__call__,

所以现在而不是执行


obj(foo,bar,baz)


有问题的代码是


obj.execute(foo,bar,baz)


在这种情况下有一个错误。根据输入,有时obj

初始化为类,有时是该类的实例。 (当我在它的时候,我修复了那个

。)问题是__call__的使用通过使实例和类可调用来掩盖底层错误




在这种特殊情况下,它显然是不必要的,只是混淆了

代码。我想知道,是否有一些一般情况下__call__方法

a用户定义的类只是必不可少的?


Thx,


Skip


I don''t personally use __call__ methods in my classes, but I have
encountered it every now and then here at work in code written by other
people. The other day I replaced __call__ with a more obvious method name,
so now instead of executing

obj(foo, bar, baz)

the code in question is

obj.execute(foo, bar, baz)

In this case there was a bug. Depending on inputs, sometimes obj
initialized to a class, sometimes an instance of that class. (I fixed that
too while I was at it.) The problem was that the use of __call__ obscured
the underlying bug by making the instance as well as the class callable.

In this particular case it was clearly unnecessary and just obfuscated the
code. I''m wondering, are there some general cases where __call__ methods of
a user-defined class are simply indispensable?

Thx,

Skip

推荐答案

在2007-08-02, sk ** @ pobox.com < sk ** @ pobox.comwrote:
On 2007-08-02, sk**@pobox.com <sk**@pobox.comwrote:

我不亲自使用我的课程中有__call__方法,但我

时不时地遇到它代码

由其他人写的。前几天我用更明显的方法名称替换了__call__

,所以现在不用执行


obj(foo,bar,baz)


有问题的代码是


obj.execute(foo,bar,baz)


在这种情况下有一个错误。根据输入,有时

obj初始化为一个类,有时是一个

类的实例。 (当我在它的时候,我也解决了这个问题。)问题是

使用__call__通过使

实例以及类可调用来掩盖底层错误。


在这种特殊情况下,它显然是不必要的,只是对代码进行了模糊处理。我想知道,是否有一些一般的

案例,其中用户定义类的__call__方法只是简单地说b
必不可少?
I don''t personally use __call__ methods in my classes, but I
have encountered it every now and then here at work in code
written by other people. The other day I replaced __call__
with a more obvious method name, so now instead of executing

obj(foo, bar, baz)

the code in question is

obj.execute(foo, bar, baz)

In this case there was a bug. Depending on inputs, sometimes
obj initialized to a class, sometimes an instance of that
class. (I fixed that too while I was at it.) The problem was
that the use of __call__ obscured the underlying bug by making
the instance as well as the class callable.

In this particular case it was clearly unnecessary and just
obfuscated the code. I''m wondering, are there some general
cases where __call__ methods of a user-defined class are simply
indispensable?



在C ++中,它们被称为仿函数,它们非常有用,因为

具有状态的代理函数。我没有看到它们在Python中以相同的方式使用它们,因为Python对(大部分)这些问题有更直接的解决方案。


这里是一个(有点傻)的例子:


class is_consecutive(object):

def __init __(self,seed ,compare = operator.lt):

self.last_value = seed

self.compare = compare

def __call __(self,total,value ):

如果总计为假:

返回False

lv = self.last_value

self.last_value =价值

返回self.compare(lv,value):


a =范围(15)

In C++ they are called functors, and they are very useful as
surrogate functions with state. I haven''t seen them used in the
same way in Python much, because Python has more immediate
solutions to (most of) these problems.

Here''s a (somewhat goofy) example:

class is_consecutive(object):
def __init__(self, seed, compare=operator.lt):
self.last_value = seed
self.compare = compare
def __call__(self, total, value):
if total is False:
return False
lv = self.last_value
self.last_value = value
return self.compare(lv, value):

a = range(15)


>> reduce(is_consecutive(0),a)
>>reduce(is_consecutive(0), a)



True

True


>> reduce(is_consecutive(0),a + [1 ,2])
>>reduce(is_consecutive(0), a + [1,2])



False


我已经有一段时间了必须是STL的心态,所以我不能想象一个更好的例子。


-

Neil Cerutti

这本书不是一本可以轻描淡写的书。应该用很棒的

力量抛出它。 --Dorothy Parker

False

It''s been a while since I had to be in the STL mindset, so I
couldn''t think of a better example.

--
Neil Cerutti
This is not a book to be put down lightly. It should be thrown with great
force. --Dorothy Parker


2007-08-02,Neil Cerutti< ho ***** @ yahoo.comwrote:
On 2007-08-02, Neil Cerutti <ho*****@yahoo.comwrote:

2007-08-02, sk**@pobox.com < sk ** @ pobox.comwrote :
On 2007-08-02, sk**@pobox.com <sk**@pobox.comwrote:

>我不会在我的课程中亲自使用__call__方法,但我偶尔会遇到它,然后在代码中工作
其他人写的。前几天我用一个更明显的方法名称替换了__call__
,所以现在不用执行

obj(foo,bar,baz)

有问题的代码是吧

obj.execute(foo,bar,baz)

在这种情况下有一个错误。根据输入,有时将obj初始化为类,有时是该类的实例。 (当我在它的时候,我也解决了这个问题。)问题是__call__的使用通过使实例以及类可调用来掩盖底层错误。
在这种特殊情况下,显然没有必要,只是对代码进行了模糊处理。我想知道,是否存在一些一般的
情况,其中用户定义类的__call__方法简直是必不可少的?
>I don''t personally use __call__ methods in my classes, but I
have encountered it every now and then here at work in code
written by other people. The other day I replaced __call__
with a more obvious method name, so now instead of executing

obj(foo, bar, baz)

the code in question is

obj.execute(foo, bar, baz)

In this case there was a bug. Depending on inputs, sometimes
obj initialized to a class, sometimes an instance of that
class. (I fixed that too while I was at it.) The problem was
that the use of __call__ obscured the underlying bug by making
the instance as well as the class callable.

In this particular case it was clearly unnecessary and just
obfuscated the code. I''m wondering, are there some general
cases where __call__ methods of a user-defined class are simply
indispensable?



在C ++中,它们被称为仿函数,它们非常有用,因为

具有状态的代理函数。我没有看到它们在Python中以相同的方式使用它们,因为Python对(大部分)这些问题有更直接的解决方案。


这里是一个(有点傻)的例子:


class is_consecutive(object):

def __init __(self,seed ,compare = operator.lt):


In C++ they are called functors, and they are very useful as
surrogate functions with state. I haven''t seen them used in the
same way in Python much, because Python has more immediate
solutions to (most of) these problems.

Here''s a (somewhat goofy) example:

class is_consecutive(object):
def __init__(self, seed, compare=operator.lt):



那应该是operator.le,而不是lt。

修复了这个bug后我忘了重新开始。

That was supposed to be operator.le, not lt. I forgot to repaste after
fixing that bug.


self.last_value = seed

self.compare =比较

def __call __(自我,总数,价值):

如果总计为假:

返回False

lv = self.last_value

self.last_value = value

返回self.compare(lv,value):


a =范围(15)
self.last_value = seed
self.compare = compare
def __call__(self, total, value):
if total is False:
return False
lv = self.last_value
self.last_value = value
return self.compare(lv, value):

a = range(15)

>>> reduce(is_consecutive(0),a)
>>>reduce(is_consecutive(0), a)



True

True


>>> reduce(is_consecutive(0), a + [1,2])
>>>reduce(is_consecutive(0), a + [1,2])



False


我已经有一段时间了必须是STL的心态,所以我不能想象一个更好的例子。

False

It''s been a while since I had to be in the STL mindset, so I
couldn''t think of a better example.



-

Neil Cerutti

牧师将传播他的告别信息,然后合唱团将唱歌,

Break Forth Into Joy。 --Church Bulletin Blooper


--
Neil Cerutti
The pastor will preach his farewell message, after which the choir will sing,
"Break Forth Into Joy." --Church Bulletin Blooper


2007年8月2日,Neil Cerutti< ho ***** @ yahoo.comwrote:
On 8/2/07, Neil Cerutti <ho*****@yahoo.comwrote:

在2007-08-02, sk**@pobox.com < sk ** @ pobox。编写:
On 2007-08-02, sk**@pobox.com <sk**@pobox.comwrote:

我不会在我的课程中亲自使用__call__方法,但我

时不时地遇到它代码工作

由其他人编写。前几天我用更明显的方法名称替换了__call__

,所以现在不用执行


obj(foo,bar,baz)


有问题的代码是


obj.execute(foo,bar,baz)


在这种情况下有一个错误。根据输入,有时

obj初始化为一个类,有时是一个

类的实例。 (当我在它的时候,我也解决了这个问题。)问题是

使用__call__通过使

实例以及类可调用来掩盖底层错误。


在这种特殊情况下,它显然是不必要的,只是对代码进行了模糊处理。我想知道,是否有一些一般的

案例,其中用户定义类的__call__方法只是简单地说b
必不可少?
I don''t personally use __call__ methods in my classes, but I
have encountered it every now and then here at work in code
written by other people. The other day I replaced __call__
with a more obvious method name, so now instead of executing

obj(foo, bar, baz)

the code in question is

obj.execute(foo, bar, baz)

In this case there was a bug. Depending on inputs, sometimes
obj initialized to a class, sometimes an instance of that
class. (I fixed that too while I was at it.) The problem was
that the use of __call__ obscured the underlying bug by making
the instance as well as the class callable.

In this particular case it was clearly unnecessary and just
obfuscated the code. I''m wondering, are there some general
cases where __call__ methods of a user-defined class are simply
indispensable?



在C ++中,它们被称为仿函数,它们非常有用,因为

具有状态的代理函数。我没有看到它们在Python中以相同的方式使用它们,因为Python对(大部分)这些问题有更直接的解决方案。


In C++ they are called functors, and they are very useful as
surrogate functions with state. I haven''t seen them used in the
same way in Python much, because Python has more immediate
solutions to (most of) these problems.



我不认为我曾经写过一篇(用Python编写)除了演示

它是如何工作的。内置函数的Pythons具有所有功能

,你必须在C ++中使用仿函数。

I don''t think I''ve ever written one (in Python) except to demonstrate
how it worked. Pythons built in functions have all the functionality
that you''d have to use a functor for in C++.


这篇关于__call__被认为是有害的还是必不可少的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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