title: Fraction class: How can define a function to add a fraction to an integer?
body: <p>I am learning the fraction class of Python and have a question below:</p>
  
  <pre><code>class Fraction:
  
       def __add__(self, other):
           newnum = self.num * other.den + self.den * other.num
           newden = self.den * other.den
           return Fraction(newnum, newden)
  
       def __radd__(self, other_int):
           newnum = self.num + self.den * other_int
           return Fraction(newnum, self.den)
  
  x = Fraction(1, 2)
  </code></pre>
  
  <p>When I write this, I got the right answer (3/2):</p>
  
  <pre><code>print(1 + x)
  </code></pre>
  
  <p>But when I write this:</p>
  
  <pre><code>print(x + 1)
  </code></pre>
  
  <p>I got the error</p>
  
  <pre class="lang-none prettyprint-override"><code>AttributeError: 'int' object has no attribute 'den'
  </code></pre>
  
  <p>Why <code>print(1 + x)</code> was correctly printed, <code>print(x + 1)</code> was error? How can I <code>print(x + 1)</code> get the answer 3/2.</p>