By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,896 Online
Bytes IT Community
+ 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
Share this Question
Share on Google+
12 Replies


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

P: n/a
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.
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
Russ wrote:
"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.
I'm not the one you were replying to, but I would say to you...
"Please write more carefully next time".

You wrote: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.


Why don't you show us your own
whatever_it_is_named_except_it_is_not_complex class?

When you are the one seeking help, you may find it helpful to be
polite.... I have found that comp.lang.python has some of the most
friendly and helpful people around. They will undoubtably help you (as
they helped me many times), if you provide answers to the questions
they ask in trying to help you...

André

Mar 9 '06 #7

P: n/a
"When you are the one seeking help, you may find it helpful to be
polite.... I have found that comp.lang.python has some of the most
friendly and helpful people around. They will undoubtably help you (as
they helped me many times), if you provide answers to the questions
they ask in trying to help you... "

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?

Mar 9 '06 #8

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
Russ wrote:
"When you are the one seeking help, you may find it helpful to be
polite.... I have found that comp.lang.python has some of the most
friendly and helpful people around. They will undoubtably help you (as
they helped me many times), if you provide answers to the questions
they ask in trying to help you... "

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?
You wrote, in message
<11**********************@i39g2000cwa.googlegroups .com>:
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.


So while you may not have implemented your own version of complex you
clearly implemented something you wanted to behave in the same way - and
that didn't.

Don't worry, let's not bother about your real problem, let's just go on
arguing about what you said or didn't say.

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.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 9 '06 #10

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
On Thu, 09 Mar 2006 09:20:17 -0800, Russ wrote:
"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.


Or why don't you explain yourself more clearly next time? You post was
unclear, and I was lead to believe that you were trying to duplicate the
complex class.

In any case, it doesn't take a genius to recognise that the question is
still a valid question if you mentally remove "complex" from the sentence.
Why don't you show us your class, whatever that class may be? At least the
relevant methods. It may help us to spot the error if we can actually see
the error...

As for your suggestion in a later post that I was asking sarcastically why
you were re-inventing the wheel, there was no sarcasm intended. I think
you are being overly-sensitive, perhaps because you realise that your
question really wasn't a good one. There *are* good reasons for
re-inventing the wheel, but there were also bad reasons, and given that I
was under the mistaken impression that you had re-created the complex
class, it was a perfectly legitimate question to ask why. You had a
problem to solve, and were asking for help on solving the problem. To give
*effective* help, we need to understand what you are trying to do.

Otherwise we mistakenly think you are trying to implement your own complex
class *wink*
--
Steven.

Mar 9 '06 #12

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.