| re: output formatting for classes
Steven D'Aprano wrote:[color=blue]
> On Fri, 10 Mar 2006 02:19:10 +0100, Schüle Daniel wrote:
>
>[color=green]
>>yeah, i miss some things in complex implementation
>>for example c=complex()
>>c.abs = 2**0.5
>>c.angle = pi/2
>>
>>should result in 1+1j :)[/color]
>
>
> Smiley noted, but consider:
>
> c = complex()
> => what is the value of c here?[/color]
default value is 0, for complex number that means
real = 0, imag = 0
is the same as
c.abs=0, c.angle=0
ok mathematically c.angle can be of arbitrary value
but defaulting it to zero is very handy
c = complex()
c.abs = 10
yields 10+0j
c=complex()
c.real = 2
c.imag = 2
c.abs = 50**0.5 # angle remains, length changed
yields 5+5j
c.angle = 0
yields 50**0.5 + 0j
[color=blue]
> c.abs = 2**0.5
> => what is c's value now?[/color]
c.abs = 2**0.5
c.angle = 0
[color=blue]
>
> c.angle = pi/2
> => now c has the value 1+1j
>
> Objects with indeterminate values are rarely a good idea.[/color]
IMHO it's perfectly consistent with[color=blue][color=green][color=darkred]
>>> int()[/color][/color][/color]
0[color=blue][color=green][color=darkred]
>>> long()[/color][/color][/color]
0L[color=blue][color=green][color=darkred]
>>> float()[/color][/color][/color]
0.0[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
complex()[color=blue][color=green][color=darkred]
>>> complex()[/color][/color][/color]
0j[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
but extending complex with default angle=0
[color=blue]
> A better way would be for complex numbers to take a constructor that can
> take arguments in either Cartesian or polar form. So, hypothetically, the
> following would all be equivalent:
>
> 1+1j
> complex(1,1)
> complex(real=1, img=1)
> complex(len=2**0.5, theta=pi/2)[/color]
ack
but after the creation of complex number one will have to
do all the transformations in another coord. system manually
[color=blue]
> Another alternative would be a function to construct polar form complex
> numbers. It could be a plain function or a static method:
>
> cmath.polar(2**0.5, pi/2) => 1+1j
> complex.polar(2**0.5, pi/2) => 1+1j[/color]
maybe adding
c=complex.from_polar((length,angle))
d=complex.to_polar(c)
d == (length, angle)
True
would be sufficient, but I would prefer the other version
Regards |