[Fwd: Re: [Tutor] OOP programming principles overview? [Complex numbersand OOP]]

Lloyd Hugh Allen lha2@columbia.edu
Wed, 21 Nov 2001 06:58:09 -0500


Darn "repy" button.

-------- Original Message --------
From: Lloyd Hugh Allen <vze2f978@mail.verizon.net>
Subject: Re: [Tutor] OOP programming principles overview?  [Complex
numbersand OOP]
To: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>

I originally missed the line "Complex numbers are already built into
Python", so in my head I was all prepared to make a long post. But now
that I see it I'll make a short one.

Just a note between the implementation of complex numbers that's built
in and the one suggested below: it appears (from inspection of results,
not internal code--I'm not that brave right now (nor do I have
sufficient free time)) that rather than storing complex numbers as a
tuple or list, a complex object simply has a "real" attribute and a
"complex" attribute:

>>> type(1+5j)
<type 'complex'>
>>> (1+5j).imag
5.0
>>> (1+5j).real
1.0

which somehow feels nicer to me than having to call imagvar.imag(). It's
a shame that with the built-in, conjugate is still a method--that is, in
order to get the conjugate of 1+5j, you have to

>>> (1+5j).conjugate()
(1-5j)

If anyone out there is interested in doing real math (pardon the pun)
with complex numbers, check out the cmath module.

Just my $0.02.

--Lloyd Hugh Allen

Danny Yoo wrote:
> 
> It might help if we browse an extended example that has some of the spirit
> of OOP, without jumping directly into OOP syntax.  If we do this, the
> concepts might become more clear.
> 
> Let's say that we're trying to make a program that helps us compute with
> complex numbers.  (Complex numbers are already built into Python, but
> they're a great example of the "object" concept --- I can't resist using
> them... *grin*)  If complex numbers give you nightmares though, we can
> write a different example if you want.
> 
> [Warning: very very long example ahead.  I need to read Strunk and White.]
...(sorry for the massive trim, Danny)