<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hello all,<br>
&nbsp;&nbsp;&nbsp; I was just messing around with a suggestion w chun (wescpy at gmail
dot com) gave (to make a class that handles time, ie:<br>
<span class="moz-txt-citetags"><br>
<tt>&gt;&gt;&gt; </tt></span><tt>import myTime<span
 class="moz-txt-citetags"><br>
&gt;&gt;&gt; </span>c = myTime.myTime(10,30)<span
 class="moz-txt-citetags"><br>
&gt;&gt;&gt; </span>print c<br>
10:30<span class="moz-txt-citetags"><br>
&gt;&gt;&gt; </span>d = myTime.myTime(8,45)<span
 class="moz-txt-citetags"><br>
&gt;&gt;&gt; </span>print c + d<br>
19:15<br>
<br>
</tt>etc), and I was trying to make it like the <tt>str</tt> class in
that, by default, when you<tt> str()</tt> a string, it <tt>return</tt>s
the same object.&nbsp; I tried to do this with the following (irrelevant
code removed), but it didn't work, despite original indications to the
contrary, and I'm not sure I understand why:<br>
<br>
<tt>&gt;&gt;&gt; class stime:<br>
&nbsp;&nbsp;&nbsp; def __init__(self, minutes, seconds=None):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if seconds is None:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if minutes.__class__ is stime:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print minutes, type(minutes), minutes.__class__,
id(minutes)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self = minutes<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print self, type(self), self.__class__, id(self)<br>
&nbsp;&nbsp;&nbsp; def __repr__(self):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return str(self)<br>
&nbsp;&nbsp;&nbsp; def __str__(self):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "%02d:%02d" % (self.minutes, self.seconds)<br>
<br>
&gt;&gt;&gt; a = stime(10,3)<br>
&gt;&gt;&gt; b = stime(a)<br>
10:03 &lt;type 'instance'&gt; __main__.stime 12858832<br>
10:03 &lt;type 'instance'&gt; __main__.stime 12858832<br>
&gt;&gt;&gt; # so, it appears to work, because it prints the same id
both times. however,<br>
# . . .<br>
&gt;&gt;&gt; id(a)<br>
12858832<br>
&gt;&gt;&gt; id(b)<br>
12858792<br>
&gt;&gt;&gt; # they're not the same anymore! and that's not all:<br>
&gt;&gt;&gt; a<br>
10:03<br>
&gt;&gt;&gt; b<br>
<br>
Traceback (most recent call last):<br>
&nbsp; File "&lt;pyshell#163&gt;", line 1, in -toplevel-<br>
&nbsp;&nbsp;&nbsp; b<br>
&nbsp; File "C:/Documents and Settings/Cookie/Desktop/stime.py", line 20, in
__repr__<br>
&nbsp;&nbsp;&nbsp; return str(self)<br>
&nbsp; File "C:/Documents and Settings/Cookie/Desktop/stime.py", line 22, in
__str__<br>
&nbsp;&nbsp;&nbsp; return "%02d:%02d" % (self.minutes, self.seconds)<br>
AttributeError: stime instance has no attribute 'minutes'<br>
<br>
</tt>So, not only do the two variables not refer to the same object
after exiting <tt>__init__</tt>, but <tt>b</tt> is now a broken <tt>stime
instance</tt>.&nbsp; Anyone know what's going on?<br>
<br>
Thanks in advance,<br>
Orri<tt></tt><!----><!----><br>
<pre class="moz-signature" cols="72">-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.</pre>
</body>
</html>