Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That feeling <em>like you should have done something else</em> it's beacuse <code>hours</code>, <code>minutes</code>, <code>seconds</code> are <strong>properties</strong>.</p> <p>You don't really need those values stored as attributes of your object, you just want to be able to access those values when you need.</p> <p>Calling something like:</p> <pre><code>&gt;&gt;&gt; t1.hours 5 </code></pre> <p>So, let's rewrite your example using <a href="http://docs.python.org/py3k/library/functions.html#property" rel="nofollow noreferrer"><code>property</code></a>:</p> <pre class="lang-py prettyprint-override"><code>class MyTime: def __init__(self, hrs=0, mins=0, secs=0): self.totalsecs = hrs*3600 + mins*60 + secs @property def hours(self): return self.totalsecs // 3600 @property def minutes(self): return self._get_leftoversecs() // 60 @property def seconds(self): return self._get_leftoversecs() % 60 def _get_leftoversecs(self): return self.totalsecs % 3600 def increment(self, t): self.totalsecs += t </code></pre> <p>Usage example:</p> <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; t1 = MyTime(5,5,5) &gt;&gt;&gt; t1.hours 5 &gt;&gt;&gt; t1.hours() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: 'int' object is not callable &gt;&gt;&gt; t1.seconds 5 &gt;&gt;&gt; t1.totalsecs 18305 &gt;&gt;&gt; t1.increment(10) &gt;&gt;&gt; t1.seconds 15 &gt;&gt;&gt; t1.totalsecs 18315 </code></pre> <p>I don't know if you noticed but <strong>you don't actually need the <code>increment</code> function anymore</strong>:</p> <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; t1.totalsecs += 10 &gt;&gt;&gt; t1.totalsecs 18325 </code></pre> <p>I know that <code>property</code> must be a little ahead of what you're doing, but I thought it would have been worth an example.</p> <p><strong>Edit:</strong> As <a href="https://stackoverflow.com/questions/9348034/python3-classes-oop-how-do-you-change-a-objects-self-values-with-a-method/9348391#comment11801734_9348391">Lattyware</a> noticed there's no need to make <code>totalsecs</code> a property too. </p> <p>To quote his comment: <em>The great thing about Python's properties is you don't need to turn everything into getters and setters to keep a consistent interface like you do in some languages.</em></p> <p>There might be an advantage in setting <code>totalsecs</code> as a property (read-only) only if for some reason you want to hide the internal implementation of <code>MyTime</code> (obviously reintegrating the <code>increment()</code> method).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload