implementation of "complex" type

R

Russ

I tried the following:
(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.
 
M

Mikael Olofsson

Russ said:
(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
 
J

Juho Schultz

Russ said:
I tried the following:



(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.
 
S

Steven D'Aprano

I tried the following:

(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.
 
C

cfbolz

Hi!
I tried the following:

(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/trunk/Objects/complexobject.c?rev=42596&view=markup

In PyPy it is indeed a pure Python implementation right now:

http://codespeak.net/svn/pypy/dist/pypy/module/__builtin__/app_complex.py

Cheers,

Carl Friedrich Bolz
 
R

Russ

"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.
 
?

=?iso-8859-1?B?QW5kcuk=?=

Russ said:
"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".
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é
 
R

Russ

"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?
 
D

Diez B. Roggisch

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
 
S

Steve Holden

Russ said:
"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
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
 
R

Russ

"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.
 
S

Steven D'Aprano

"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*
 
R

Russ

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top