473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

implementation of "complex" type

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 1542
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
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
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
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
"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
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
"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
> 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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
5
by: Brian Angliss | last post by:
I'm relatively new to scripting in JavaScript, so I'm not too surprised I'm having difficulty scripting up an animation effect for my personal site. What I'm trying to do is the following: When...
3
by: Susie Swint | last post by:
I have the following IIf statement which worked in Access 95 but will not work in Access 2002. The error message I get is that the expression is typed incorrectly, or is too complex to be...
8
by: Matt | last post by:
Hi all, Thank you for taking the time. I have a database with 45 tables on it. 44 tables are linked to a main table through a one to one relationship. My question is, is there no way i can...
4
by: Mantorok Redgormor | last post by:
With what specifier do I use to print a variable of a complex type? The man page for printf doesn't even say. - nethlek
18
by: njgreen2001 | last post by:
I thought the length of this might give me problems. Any suggestions on how to shorten it? Expression: =IIf(="A Pos" And ="A Pos",(DLookUp("","")), IIf(="A Pos" And ="A...
7
by: =?iso-8859-1?Q?S=F8ren_M._Olesen?= | last post by:
Hi How do I pass a complex type to a webservice?? What I have is a Class 'MyComplexClass' which lives in it's own dll/namespace. I'd like to pass this class to my webmethod: <WebMethod()_...
1
by: wayniac | last post by:
I am having a problem when I try and remove several fields from the lookup. I have many fields, and the error I am getting is "Expression is too complex", is there anyway of going around this or...
1
by: Rahul Babbar | last post by:
Hi, I ran the scripts in a file from Command Line Processor and it gave the error for all the constraints being added, but not the indexes being added. For a simple statement like Alter...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.