output formatting for classes

R

Russ

I'd like to get output formatting for my own classes that mimics the
built-in output formatting. For example,
4.54

In other words, if I substitute a class instance for "x" above, I'd
like to make the format string apply to an element or elements of the
instance. Can I somehow overload the "%" operator for that? Thanks.

On an unrelated matter, I think the complex class in Python is too
complex, so I plan to clean it up and implement it right. (just
kidding, folks!)
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Russ said:
I'd like to get output formatting for my own classes that mimics the
built-in output formatting. For example,



4.54

In other words, if I substitute a class instance for "x" above, I'd
like to make the format string apply to an element or elements of the
instance. Can I somehow overload the "%" operator for that? Thanks.

On an unrelated matter, I think the complex class in Python is too
complex, so I plan to clean it up and implement it right. (just
kidding, folks!)

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 :)

or c=complex(1,1)
print c.abs # should print 2**0.5
print c.angle # should print pi%2

i think one can implement it with properties

but to your question ...
.... def __float__(self):
.... return 1.0
.... def __long__(self):
.... return 10l
.... def __int__(self):
.... return 20
.... def __repr__(self):
.... return "i am"
.... def __str__(self):
.... return "I AM"
.... def __complex__(self):
.... return 1+1j
....
 
S

Steven D'Aprano

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 :)

Smiley noted, but consider:

c = complex()
=> what is the value of c here?

c.abs = 2**0.5
=> what is c's value now?

c.angle = pi/2
=> now c has the value 1+1j

Objects with indeterminate values are rarely a good idea.

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)

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
 
G

Guest

Steven said:
Smiley noted, but consider:

c = complex()
=> what is the value of c here?

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
c.abs = 2**0.5
=> what is c's value now?

c.abs = 2**0.5
c.angle = 0
c.angle = pi/2
=> now c has the value 1+1j

Objects with indeterminate values are rarely a good idea.

IMHO it's perfectly consistent with
but extending complex with default angle=0


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)

ack
but after the creation of complex number one will have to
do all the transformations in another coord. system manually
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

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
 

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

Forum statistics

Threads
474,289
Messages
2,571,448
Members
48,126
Latest member
ToneyChun2

Latest Threads

Top