根据需要更改__call__ [英] changing __call__ on demand

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

问题描述

嗨!


这让我有些困惑:


Python 2.4(#1,2005年2月3日,16:47:05)

[GCC 3.3.4(前3.3.5 20040809)] on linux2

输入help,copyright,credit等等。或许可证或欲了解更多信息。


..>>> class test(object):

.... def __init __(self):

.... self .__ call__ = self .__ call1

.... def __call1(个体经营):

....打印1

.... def __call __(个体经营):

....打印2

....

..>>> t = test()

..>>> t()

2


如果我完全取出__call__方法而只在__init__中设置它,我将获得一个
TypeError表示测试不可调用。

我想使用它来提供基于

对象配置的不同实现。计算正确的调用函数非常重要

并且调用频繁,所以我想更改__call__以便直接运行正确的

函数。


我知道,我可以使用另一个间接级别:


def __call __(自我):

self.the_right_method()


然后相应地设置the_right_method,但我发现有点

次优。有没有办法在课程创建后改变__call__?


Stefan

解决方案

我试过这个:< br>

class test(object):
.... def __call1(self):

....打印1

.... __ call__ = __call1

.... t = test()
t()
1



这就是你要找的东西吗?


-

Alan McIntyre

ESRG LLC
http://www.esrgtech .com


Stefan Behnel写道:嗨!

这让我感到很困惑:

Python 2.4(#1 ,2005年2月3日,16:47:05)
在Linux2上的[GCC 3.3.4(前3.3.5 20040809)]
类型帮助,版权,信用等。或许可证或了解更多信息。

。>>> class test(object):
... def __init __(self):
... self .__ call__ = self .__ call1
... def __call1(self):
...打印1
... def __call __(自我):
...打印2
...
。>>> t = test()
。>>> t()
2
如果我完全取出__call__方法并且只在
__ init__中设置它,我会得到一个TypeError,说测试不可调用。

我想使用它来为对象配置提供基于
的不同实现。计算正确的调用函数是非常重要的,并且调用很频繁,所以我想在
命令中更改__call__以直接运行正确的函数。

我知道,我可以使用另一个间接级别:

def __call __(self):
self.the_right_method()

然后相应地设置the_right_method,但我发现有点<次优的。有没有办法在课程创建后改变__call__?

Stefan




Stefan Behnel写道:

有没有办法在课程创建后更改__call__?




__call__,如__getitem__,而__getattr__在
$ b $上调用b类对象,而不是实例对象。所以,不,不是我想知道的,而不是使用元类欺骗。最简单的选择

IMO将按照你的建议使用另一个间接级别。

-

Michael Hoffman


Alan McIntyre写道:

class test(object):


... def __call1(个体经营):
...打印1
... __call__ = __call1
这就是你要找的吗?




这仍然只允许他每班有一个电话功能。

-

Michael Hoffman


Hi!

This somewhat puzzles me:

Python 2.4 (#1, Feb 3 2005, 16:47:05)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

..>>> class test(object):
.... def __init__(self):
.... self.__call__ = self.__call1
.... def __call1(self):
.... print 1
.... def __call__(self):
.... print 2
....
..>>> t = test()
..>>> t()
2

If I take out the __call__ method completely and only set it in __init__, I
get a TypeError saying that test is not callable.

I want to use this in order to provide different implementations based on the
object configuration. Calculating the right function to call is non-trivial
and calls are frequent, so I want to change __call__ in order to run the right
function directly.

I know, I could use another level of indirection:

def __call__(self):
self.the_right_method()

and then set the_right_method accordingly, but I find that somewhat
sub-optimal. Is there a way to change __call__ after class creation?

Stefan

解决方案

I tried this:

class test(object): .... def __call1(self):
.... print 1
.... __call__ = __call1
....t = test()
t() 1


Is that what you were looking for?

--
Alan McIntyre
ESRG LLC
http://www.esrgtech.com

Stefan Behnel wrote: Hi!

This somewhat puzzles me:

Python 2.4 (#1, Feb 3 2005, 16:47:05)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

.>>> class test(object):
... def __init__(self):
... self.__call__ = self.__call1
... def __call1(self):
... print 1
... def __call__(self):
... print 2
...
.>>> t = test()
.>>> t()
2

If I take out the __call__ method completely and only set it in
__init__, I get a TypeError saying that test is not callable.

I want to use this in order to provide different implementations based
on the object configuration. Calculating the right function to call is
non-trivial and calls are frequent, so I want to change __call__ in
order to run the right function directly.

I know, I could use another level of indirection:

def __call__(self):
self.the_right_method()

and then set the_right_method accordingly, but I find that somewhat
sub-optimal. Is there a way to change __call__ after class creation?

Stefan




Stefan Behnel wrote:

Is there a way to change __call__ after class creation?



__call__, like __getitem__, and __getattr__ is called on the
class object, not the instance object. So, no, not as far as I
am aware, without using metaclass trickery. The simplest option
IMO is to use another level of indirection as you suggest.
--
Michael Hoffman


Alan McIntyre wrote:

class test(object):


... def __call1(self):
... print 1
... __call__ = __call1
Is that what you were looking for?



That still only allows him to have one call function per class.
--
Michael Hoffman


这篇关于根据需要更改__call__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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