Python: Printing value after .upper
<p>I am trying to print a string in all uppercase letters. When I run the print command it prints the type of x and the location. </p>

<p>Why does it print the operation instead of the result?</p>

<pre><code>x = 'bacon'
x = x.upper
print x

&gt;&gt;&gt; 
&lt;built-in method upper of str object at 0x02A95F60&gt;
</code></pre>

<p>Everything in Python is an object, including functions and methods. <code>x.upper</code> is the <code>upper</code> attribute of <code>x</code>, which happens to be a function object. <code>x.upper()</code> is the result of trying to call that attribute as a function, so you are trying to do</p>

<pre><code>print x.upper()
</code></pre>

<p>As an aside, you can try to call any object in Python, not just functions. It will not always work, but the syntax is legitimate. You can do</p>

<pre><code>x = 5
x()
</code></pre>

<p>or even</p>

<pre><code>5()
</code></pre>

<p>but of course you will get an error (the exact same one in both cases). However, you can actually call objects as functions as long as they define a <code>__call__</code> method (as normal functions do). Your example can actually be rewritten as</p>

<pre><code>print x.upper.__call__()
</code></pre>

python python-2.7