468,736 Members | 1,993 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,736 developers. It's quick & easy.

output formatting for classes

I'd like to get output formatting for my own classes that mimics the
built-in output formatting. For example,
x = 4.54
print "%4.2f" % x

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

Mar 10 '06 #1
3 1792
Russ wrote:
I'd like to get output formatting for my own classes that mimics the
built-in output formatting. For example,

x = 4.54
print "%4.2f" % x
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 ...
class X(object): .... 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
.... x=X()
int(x) 20 long(x) 10L float(x) 1.0 str(x) 'I AM' repr(x) 'i am' print "%s -- %r" % (x,x) I AM -- i am complex(x) (1+1j)


Mar 10 '06 #2
On Fri, 10 Mar 2006 02:19:10 +0100, Schüle Daniel wrote:
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

--
Steven.

Mar 11 '06 #3
Steven D'Aprano wrote:
On Fri, 10 Mar 2006 02:19:10 +0100, Schüle Daniel wrote:

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?


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
int() 0 long() 0L float() 0.0 complex() complex() 0j


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

Mar 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Lisa | last post: by
2 posts views Thread by djc | last post: by
11 posts views Thread by Russ | last post: by
6 posts views Thread by Jojo | last post: by
6 posts views Thread by Ralf Goertz | last post: by
1 post views Thread by jasonterando | last post: by
5 posts views Thread by thedsadude | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.