English 中文(简体)
自定义添加方法在字符串内插期间失败
原标题:Custom addition method fails during string interpolation
#it s python 3.2.3
class point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, point):
        self.x += point.x
        self.y += point.y
        return self

    def __repr__(self):
        return  point(%s, %s)  % (self.x, self.y)

class Test:
    def __init__(self):
        self.test1 = [point(0, i) for i in range(-1, -5, -1)]
        self.test2 = [point(i, 0) for i in range(-1, -5, -1)]

        print( %s
+
%s
=
%s  % (self.test1[0], self.test2[0], self.test1[0] + self.test2[0]))

test = Test()
input()

此程序的输出为

point(-1, -1)
+
point(-1, 0)
=
point(-1, -1)

但它应该是

point(-1, -1)
+
point(-1, 0)
=
point(-2, -1)

但如果我这样做

print(point(-1, -1) + point(-1, 0))

工作很顺利

我想知道为什么 以及如何解决这个问题

对不起,如果我的英语不好:)

最佳回答

您的 函数将左手参数修改为 。 例如 :

>>> x = point(0, 0)

>>> x + point(1, 1)
point(1, 1)

>>> x
point(1, 1)

您应该更改 的像

def __add__(self, oth):
    return point(self.x + oth.x, self.y + oth.y)
问题回答

你说产出应该是:

point(-1, -1)
+
point(-1, 0)
=
point(-2, -1)

事实上,它应该是:

point(0, -1)
+
point(-1, 0)
=
point(-1, -1)

因为您正在以 [point( 0, i) 为 i 创建范围( 1, 5, - 1) (注意 x 参数为 < code>0 ) 的第一个点 。

这是 (self. test1[0], self. test2[0], self. test1[0] + self. test2[0])) 被评估的结果 - 添加修改图普尔的第一个点( 它们是同一个对象 ) 。 这也是为什么您的第二个示例工作正确( 或看起来正确) - 您只打印一次修改对象 。

执行您提供的 适合 执行 运算符。 正确执行应创建一个全新的 point 对象 :

def __add__(self, oth):
    return point(self.x + oth.x, self.y + oth.y)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...