What is the default value that Python chooses when adding two variables?
<p>Suppose we have the following context: </p>

<pre><code>x = 3  
y = 4  
z = x + y
</code></pre>

<p>Will <code>z</code> be an <code>int</code> or <code>float</code>, etc? I am well aware that floating numbers do end with <code>.something</code>, however its unclear to me whether Python will favour the floating point type over the integer type, given the circumstances that it is unpredictable whether the user is going to change this variable to another type or not.</p>

<p>The <em>mixing</em> rules Martijn described are a good way to think about built in numeric types. More broadly, any object can support arithmetic operators such as <code>+</code> and <code>*</code> as long as the methods are implemented. With objects <code>a</code> and <code>b</code> when you apply an arithmetic operation like <code>a + b</code> python will call <code>a.__add__(b)</code>. If that function returns <code>NotImplemented</code> then python will fall back on <code>b.__radd__(a)</code>. Using this approach it is possible for only one of the two objects to know how to work with the other, and choose to act like the <em>wider</em> type.</p>

<p>For example the work for <code>4.0 + 3</code> is done by the float, and <code>3 + 4.0</code> is <em>also</em> computed by the float. To make your own numeric type that in some sense <em>wider</em> than a built in you all you need to do is implement your own <code>__add__</code> and <code>__radd</code>__ methods that returns your wider type. This relies on the builtins returning <code>NotImplemented</code> for an object they don't recognize.</p>

python python-3.x types