“复杂”的实施类型 [英] implementation of "complex" type
问题描述
我尝试了以下方法:
x = complex(4)
y = x
y * = 2
print x,y
(4 + 0j)(8 + 0j)
但是当我用自己的班级代替
" complex上面,我发现x和y都加倍了。我想
让我的课堂表现得像复杂一样。类。有人能告诉我
的伎俩吗?此外,我在哪里可以找到复杂的代码。类?我希望它是用Python编写的
!谢谢。
Russ写道:x =复杂(4)
y = x
y * = 2
打印x,y
( 4 + 0j)(8 + 0j)
但是当我用自己的班级尝试相同的东西代替
复杂时上面,我发现x和y都加倍了。我想让我的课堂表现得像复杂一样。类。有人可以告诉我
技巧吗?此外,我在哪里可以找到复杂的代码。类?我希望它是用Python编写的!谢谢。
你的__imul__和这样的返回新对象,而不是就地执行
修改如果你不想要x和y在y * = 2之后引用相同的对象
。
/ MiO
Russ写道:我尝试了以下内容:
x = complex(4)
y = x
y * = 2
打印x,y
(4 + 0j)(8 + 0j)
复杂时上面,我发现x和y都加倍了。我想让我的课堂表现得像复杂一样。类。有人可以告诉我
技巧吗?此外,我在哪里可以找到复杂的代码。类?我希望它是用Python编写的!谢谢。
这就像元组和列表的区别。
你自己的班级是可变的。
y = x#名称x和y现在绑定到同一个对象。
y * = 2#更改绑定到名称x和y的对象。
内置复合体是不可变的,因此你无法操纵内容。
y * = 2#创建一个新对象(值= 2 * y),将其绑定到名称y。
2006年3月9日星期四01:15:57 -0800,Russ写道:
我尝试了以下内容:
< blockquote class =post_quotes>x = complex(4)
y = x
y * = 2
print x,y
(4 + 0j)(8 + 0j)
但是当我用自己的班级尝试同样的东西代替
复杂时上面,我发现x和y都加倍了。我想让我的课堂表现得像复杂一样。类。有人可以告诉我
技巧吗?此外,我在哪里可以找到复杂的代码。类?我希望它是用Python编写的!谢谢。
或者,换句话说:
"这里有一些有用的代码!
现在,我已经编写了一些*其他*代码,其中有一个错误。谁想知道这个bug是什么?
为什么你不向我们展示你的复杂课程?
另外,你为什么重新发明轮子?只是学习
语言?或者因为您想添加额外的功能?或者只是NIH
综合症?如果是学习练习,我会为你鼓掌;如果是为了
添加额外的功能,有更好的方法。
-
史蒂文。
I tried the following:
x = complex(4)
y = x
y *= 2
print x, y
(4+0j) (8+0j)
But when I tried the same thing with my own class in place of
"complex" above, I found that both x and y were doubled. I''d like to
make my class behave like the "complex" class. Can someone tell me the
trick? Also, where can I find the code for for the "complex" class? I
hope it''s written in Python! Thanks.
Russ wrote:x = complex(4)
y = x
y *= 2
print x, y
(4+0j) (8+0j)
But when I tried the same thing with my own class in place of
"complex" above, I found that both x and y were doubled. I''d like to
make my class behave like the "complex" class. Can someone tell me the
trick? Also, where can I find the code for for the "complex" class? I
hope it''s written in Python! Thanks.
Have your __imul__ and such return new objects, and not perform in-place
modification if you do not want x and y to refer to the same object
after y*=2.
/MiO
Russ wrote:I tried the following:x = complex(4)
y = x
y *= 2
print x, y
(4+0j) (8+0j)
But when I tried the same thing with my own class in place of
"complex" above, I found that both x and y were doubled. I''d like to
make my class behave like the "complex" class. Can someone tell me the
trick? Also, where can I find the code for for the "complex" class? I
hope it''s written in Python! Thanks.
This is like the difference of tuples and lists.
Your own class is mutable.
y=x # Names x and y are now bound to the same object.
y*=2 # change the object bound to names x and y.
Builtin complex is immutable, so you can not manipulate the contents.
y*=2 # creates a new object (value = 2*y), binds it to name y.
On Thu, 09 Mar 2006 01:15:57 -0800, Russ wrote:
I tried the following:x = complex(4)
y = x
y *= 2
print x, y
(4+0j) (8+0j)
But when I tried the same thing with my own class in place of
"complex" above, I found that both x and y were doubled. I''d like to
make my class behave like the "complex" class. Can someone tell me the
trick? Also, where can I find the code for for the "complex" class? I
hope it''s written in Python! Thanks.
Or, to put it another way:
"Here''s some code that works!
Now, I''ve written some *other* code, which has a bug in it. Who''d like to
guess what the bug is?"
Why don''t you show us your complex class?
Also, why are you re-inventing the wheel? Is it just to learn the
language? Or because you want to add extra functionality? Or just NIH
Syndrome? If it is a learning exercise, they I applaud you; if it is to
add extra functionality, there is a better way.
--
Steven.
这篇关于“复杂”的实施类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!