很常见的一种情况是,类似结构的类型不希望被远程版权持有者修改。在
字符串是一个基本的例子,但这是一个简单的例子,因为它是可以原谅的不可变的——Python甚至不允许对文本字符串进行方法调用之类的事情。在
问题是(在大多数语言中)我们经常有这样的东西,比如(x,y)
Point
类。我们偶尔想独立地改变x
和{Point
左值应该是可变的(即使副本看不到变化)。在
但Python2.7似乎没有提供任何选项来启用赋值时自动复制。所以这意味着我们实际上必须使我们的Point
类不可变,因为不经意的引用会到处被创建(通常是因为有人在将对象传递给其他人之前忘记了克隆对象)。在
不,我对无数的黑客攻击不感兴趣,这些黑客只允许在“创建对象的同时”进行变异,因为这是一个不可伸缩的弱概念。在
这些情况的逻辑结论是,我们需要我们的变异方法来实际修改左值。例如,%=
支持这一点。问题是有一个更合理的语法会更好,比如使用__setattr__
和/或定义set_x
和{
class Point(object):
# Python doesn't have copy-on-assignment, so we must use an immutable
# object to avoid unintended changes by distant copyholders.
def __init__(self, x, y, others=None):
object.__setattr__(self, 'x', x)
object.__setattr__(self, 'y', y)
def __setattr__(self, name, value):
self %= (name, value)
return self # SHOULD modify lvalue (didn't work)
def __repr__(self):
return "(%d %d)" % (self.x, self.y)
def copy(self, x=None, y=None):
if x == None: x = self.x
if y == None: y = self.y
return Point(x, y)
def __eq__ (a,b): return a.x == b.x and a.y == b.y
def __ne__ (a,b): return a.x != b.x or a.y != b.y
def __add__(a,b): return Point(a.x+b.x, a.y+b.y)
def __sub__(a,b): return Point(a.x-b.x, a.y-b.y)
def set_x(a,b): return a.copy(x=b) # SHOULD modify lvalue (didn't work)
def set_y(a,b): return a.copy(y=b) # SHOULD modify lvalue (didn't work)
# This works in Python 2.7. But the syntax is awful.
def __imod__(a,b):
if b[0] == 'x': return a.copy(x=b[1])
elif b[0] == 'y': return a.copy(y=b[1])
else: raise AttributeError, \
"Point has no member '%s'" % b[0]
my_very_long_and_complicated_lvalue_expression = [Point(10,10)] * 4
# modify element 0 via "+=" -- OK
my_very_long_and_complicated_lvalue_expression[0] += Point(1,-1)
# modify element 1 via normal "__set_attr__" -- NOT OK
my_very_long_and_complicated_lvalue_expression[1].x = 9999
# modify element 2 via normal "set_x" -- NOT OK
my_very_long_and_complicated_lvalue_expression[2].set_x(99)
# modify element 3 via goofy "set_x" -- OK
my_very_long_and_complicated_lvalue_expression[3] %='x', 999
print my_very_long_and_complicated_lvalue_expression
结果是:
^{pr2}$如您所见,+=
和%=
工作得很好,但是其他任何东西似乎都不起作用。当然,语言发明者已经为左值修改创建了一个基本的语法,而不仅仅局限于看起来愚蠢的运算符。我就是找不到。请帮忙。在
在Python中,典型的模式是在修改之前进行复制,而不是在赋值时复制。你可以用你想要的语义来实现某种数据存储,但这似乎需要大量的工作。在
我觉得我们已经尽职尽责地寻找已经存在的解决方案。鉴于“<;=”在某些语言中是赋值(例如,Verilog),我们可以非常直观地介绍:
作为Python的形式
^{pr2}$以下是一个更新的示例,以供参考:
相关问题 更多 >
编程相关推荐