443,617 Members | 1,896 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

# implementation of "complex" type

 P: n/a 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. Mar 9 '06 #1
12 Replies

 P: n/a Russ wrote:x = complex(4)y = xy *= 2print 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 Mar 9 '06 #2

 P: n/a Russ wrote: I tried the following:x = complex(4)y = xy *= 2print 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. Mar 9 '06 #3

 P: n/a 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. Mar 9 '06 #4

 P: n/a Hi! 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. In CPython it is actually written in C, implemented in the Objects/complexobject.c file. See for example: http://svn.python.org/view/python/tr...96&view=markup In PyPy it is indeed a pure Python implementation right now: http://codespeak.net/svn/pypy/dist/p...app_complex.py Cheers, Carl Friedrich Bolz Mar 9 '06 #5

 P: n/a "Why don't you show us your complex class?" Because I don't have a complex class. I merely used the complex class as an example to test the referencing behavior. Please read more carefully next time. Mar 9 '06 #6

 P: n/a > Asking sarcastically why I am "reinventing the wheel" is a funny way of being friendly. I said absolutely nothing to indicate that I had re-implemented my own version of complex. And, by the way, even if I had, so what? Why is it even worth commenting on? Well - obviously its not worth commenting on your posts. Diez Mar 9 '06 #9

 P: n/a "Better still, let's not bother ... I'm sorry you don't like the support around here, I guess you get what you ask for. " Boy, some people are very sensitive around here. And some of them also have a bad habit of misrepresenting my views. For the record, I am very happy with the support here. In fact, I solved the problem thanks to a couple of helpful replies to my query, and I certainly appreciate that help. What I don't appreciate is having people leap to unwarranted assumptions and falsely claim that I don't appreciate the help I get. I can do without that, thanks. Mar 9 '06 #11

 P: n/a Thanks for the links, especially for the pure Python implementation. That provides a good model for similar classes. I am just wondering why your implementation of complex numbers does not have "assignment operators" such as "__iadd", etc. By the way, I suppose my original post (where I wrote, "I'd like to make my class behave like the "complex" class.") could be construed to mean that I was implementing my own version of the complex class. But I only meant that I want it to "behave" like the built-in complex class with regard to referencing and copying (as my code example showed). My apologies for any misunderstanding that occurred. Mar 9 '06 #13

### This discussion thread is closed

Replies have been disabled for this discussion.