Python 是否不允许print
在类方法名称中使用(或其他保留字)?
$ cat a.py
import sys
class A:
def print(self):
sys.stdout.write("I'm A\n")
a = A()
a.print()
$ python a.py
File "a.py", line 3
def print(self):
^
SyntaxError: invalid syntax
更改print
为其他名称(例如,aprint
)不会产生错误。如果有这样的限制,我感到很惊讶。在 C++ 或其他语言中,这不是问题:
#include<iostream>
#include<string>
using namespace std;
class A {
public:
void printf(string s)
{
cout << s << endl;
}
};
int main()
{
A a;
a.printf("I'm A");
}