重载赋值运算符 [英] Overloading assignment operator
问题描述
我想用Python编写应用程序中的一些公式。用户
应该可以写出类似的东西
A = B * C
其中A,B ,C是一些包装类的实例。重载*是没有问题但是我不能超载A的赋值。我明白这是因为Python的本质,但有一个解决方法
这个?
所有我感兴趣的是一个干净的语法来编写我的应用程序脚本。任何想法都很受欢迎。
问候,
Achim
Achim Domma写道:
我想用Python编写我的应用程序中的一些公式。用户
应该可以写出类似的东西
A = B * C
其中A,B ,C是一些包装类的实例。重载*是没有问题但是我不能超载A的赋值。我明白这是因为Python的本质,但有一个解决方法
这个?
>> class D(dict):
.... def __setitem __(self,key,value):
.... print key," ;< - ",value
.... dict .__ setitem __(自我,钥匙,价值)
....
>> namespace = D(B = 42,C = 24)
exec" A = B * C在命名空间
A< - 1008
Peter
1月23日下午12:24,Achim Domma< d ... @ procoders.netwrote:
我想用Python编写应用程序中的一些公式。用户
应该可以写出类似的东西
A = B * C
其中A,B ,C是一些包装类的实例。重载*是没有问题但是我不能超载A的赋值。我明白这是因为Python的本质,但有一个解决方法
这个?
所有我感兴趣的是一个干净的语法来编写我的应用程序脚本。任何想法都很受欢迎。
问候,
Achim
简单选项:您如何使用''<< =< =''而不是''=''(通过定义__ilshift__来定义
)?这给你:
A<< = B * C
(看起来有点像注入B的结果时间C进入A)
更复杂的选项:将表达式/赋值解析器嵌入到你的应用程序的
中。你可以使用一些带有pyparsing的示例来获得跳跃(可以在线检查这些 http://pyparsing.wikispaces.com/Examples - 查看fourFn.py和
simpleArith.py)。 />
- Paul
Achim Domma写道:
我想用Python在我的应用程序中编写一些公式。用户
应该可以写出类似的东西
A = B * C
其中A,B ,C是一些包装类的实例。重载*是没有问题但是我不能超载A的赋值。我明白这是因为Python的本质,但有一个解决方法
这个?
所有我感兴趣的是一个干净的语法来编写我的应用程序脚本。任何想法都很受欢迎。
非常欢迎。
你确定你甚至需要这样做吗?
>> C类:
.... def __init __(self,x) :
.... self.x = x
.... def __mul __(自我,其他):
....返回C(self.x * other.x)
....
>>结果= C(2)* C(3)
打印结果
< __ main __。位于0x402e13ec的C实例>
>> result.x
6
-
Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/
美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM,Y!M erikmaxfrancis
生活是无法入睡的事情。
- Fran Lebowitz
Hi,
I want to use Python to script some formulas in my application. The user
should be able to write something like
A = B * C
where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I''m interested in is a clean syntax to script my app. Any ideas are
very welcome.
regards,
Achim
Achim Domma wrote:
I want to use Python to script some formulas in my application. The user
should be able to write something like
A = B * C
where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
>>class D(dict):
.... def __setitem__(self, key, value):
.... print key, "<--", value
.... dict.__setitem__(self, key, value)
....>>namespace = D(B=42, C=24)
exec "A = B * C" in namespace
A <-- 1008
Peter
On Jan 23, 12:24 pm, Achim Domma <d...@procoders.netwrote:Hi,
I want to use Python to script some formulas in my application. The user
should be able to write something like
A = B * C
where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I''m interested in is a clean syntax to script my app. Any ideas are
very welcome.
regards,
AchimSimple option: how do you feel about using ''<<='' instead of ''='' (by
defining __ilshift__)? This gives you:
A <<= B * C
(looks sort of like "injecting" the result of B times C into A)
More complicated option: embed an expression/assignment parser into
your app. You can get a jump on this using some of the examples that
come with pyparsing (can check these out online at
http://pyparsing.wikispaces.com/Examples - look at fourFn.py and
simpleArith.py).
-- Paul
Achim Domma wrote:
I want to use Python to script some formulas in my application. The user
should be able to write something like
A = B * C
where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I''m interested in is a clean syntax to script my app. Any ideas are
very welcome.Are you sure you even need to do that?
>>class C:
.... def __init__(self, x):
.... self.x = x
.... def __mul__(self, other):
.... return C(self.x*other.x)
....>>result = C(2)*C(3)
print result
<__main__.C instance at 0x402e13ec>
>>result.x
6
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Life is something to do when you can''t get to sleep.
-- Fran Lebowitz
这篇关于重载赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!