Using print as class method name in Python
<p>Does Python disallow using <code>print</code> (or other reserved words) in class method name?</p>

<p><code>$ cat a.py</code></p>

<pre><code>import sys
class A:
    def print(self):
        sys.stdout.write("I'm A\n")
a = A()
a.print()
</code></pre>

<p><code>$ python a.py</code></p>

<pre><code>File "a.py", line 3
  def print(self):
          ^
  SyntaxError: invalid syntax
</code></pre>

<p>Change <code>print</code> to other name (e.g., <code>aprint</code>) will not produce the error. It is surprising to me if there's such restriction. In C++ or other languages, this won't be a problem:</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;string&gt;
using namespace std;

class A {
  public:
    void printf(string s)
    {
      cout &lt;&lt; s &lt;&lt; endl;
    }
};


int main()
{
  A a;
  a.printf("I'm A");
}
</code></pre>

<p>The restriction is gone in Python 3, when print was changed from a statement to a function.  Indeed, you can get the new behaviour in Python 2 with a future import:</p>

<pre><code>&gt;&gt;&gt; from __future__ import print_function
&gt;&gt;&gt; import sys
&gt;&gt;&gt; class A(object):
...     def print(self):
...         sys.stdout.write("I'm A\n")
...     
&gt;&gt;&gt; a = A()
&gt;&gt;&gt; a.print()
I'm A
</code></pre>

<p>As a style note, it's unusual for a python class to define a <code>print</code> method.  More pythonic is <em>return</em> a value from the <code>__str__</code> method, which customises how an instance will display when it is printed.  </p>

<pre><code>&gt;&gt;&gt; class A(object):
...     def __str__(self):
...         return "I'm A"
...     
&gt;&gt;&gt; a = A()
&gt;&gt;&gt; print(a)
I'm A
</code></pre>

python printing reserved