Stephen McInerney wrote:
Two questions about complex numbers:
 
a) why are methods __add__,__mul__ defined but not the operators  '+','-','*','/' ?

I must be missing something. There operators work for me:

>>> a=1+2j
>>> b=2+3j
>>> a+b, a-b, a*b, a/b
((3+5j), (-1-1j), (-4+7j), (0.61538461538461542+0.076923076923076913j))



b) Say I have a list ll which mixes real (float) and complex numbers.
ll = [1, 0.80-0.58j, 0.11+.2j]
What is the best Python idiom for getting the list of real parts of elements of ll?

[[complex(lx).real for lx in ll]

[lx.real for lx in ll] fails because only complex numbers have a 'real'
attribute, real numbers don't (slightly bizarre).

Frankly I'd enjoy reading your posts if you would be less critical of the implementation. It is what it is and it does not meet some of your needs and that is not the fault of the langauage.
Unless I write the not-so-readable:
[(type(lx) is complex and lx.real or lx) for lx in ll]
or define my own function.
 
Do any of you see a need for a math.real() (or math.complex())
function, which operates on all numeric types?
Then I could simply take the list comprehension
[real(lx) for lx in ll]
Is this worthy of a PEP?

FWIW I'd like to see all numbers have real and imag attributes. Regarding "worthiness" - the language grows as suggestions are offered. I'd say write and submit one.
It seems simple but overlooked.
 

-- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to