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><strong>print</strong> is a reserved word in Python 2.x, so you can't use it as an identifier. Here is a list of reserved words in Python: <a href="https://docs.python.org/2.5/ref/keywords.html" rel="nofollow">https://docs.python.org/2.5/ref/keywords.html</a>.</p>

python printing reserved