<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Stephen McInerney wrote:
<blockquote cite="mid:BAY111-W5355B1998C18F4BF4139DDA04E0@phx.gbl"
type="cite">
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style> <br>
Why does the complex.__str__() method on complex numbers add the
enclosing parentheses?<br>
</blockquote>
<br>
Because it was designed that way and then implemented to follow the
design.<br>
<br>
Or are you asking why it was designed that way? I guess you'd have to
ask the designer.<br>
<br>
At least it is somewhat compatible with str representations of other
data types that contain more than 1 value, e.g. decimal, set, tuple, ...<br>
<br>
<blockquote cite="mid:BAY111-W5355B1998C18F4BF4139DDA04E0@phx.gbl"
type="cite">It's unwanted</blockquote>
<br>
By some; others like it. I think you mean you don't want it.<br>
<br>
<blockquote cite="mid:BAY111-W5355B1998C18F4BF4139DDA04E0@phx.gbl"
type="cite">, and it also makes them look like a tuple (other than the
trailing comma).<br>
</blockquote>
<br>
True.<br>
<br>
<blockquote cite="mid:BAY111-W5355B1998C18F4BF4139DDA04E0@phx.gbl"
type="cite">How can I get rid of it, other than the clunky:<br>
</blockquote>
<br>
Please define "clunky". <br>
<blockquote cite="mid:BAY111-W5355B1998C18F4BF4139DDA04E0@phx.gbl"
type="cite"> <br>
>>> print d<br>
(0.80-0.58j)<br>
>>> print repr(d)[1:-1]<br>
0.80-0.58j<br>
<br>
How can I change complex.__str__() ?<br>
</blockquote>
Create your own subclas<br>
<br>
>>> class C(complex):<br>
... def __str__(self):<br>
... return "%s-%sj" % (self.real, self.imag)<br>
... def __init__(self, r, i):<br>
... complex.__init__(self, r, i)<br>
... <br>
>>> c=C(1,2)<br>
>>> c<br>
(1+2j)<br>
>>> print c<br>
'1.0-2.0j';<br>
<br>
This might seem clunky but this is the way to alter behavior of
built-in objects.<br>
<br>
<pre class="moz-signature" cols="72">--
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?
</pre>
</body>
</html>