class.print method illegal

L

Larry Bates

I just ran into something I've not seen before:

class foo:
def print(self):
...do something...

creates a syntax error. Why can't a class have
a method called "print"? Doesn't seem to be
any way it could be confused with the reserved
print statement, or am I missing something?

Regards,
Larry Bates
 
D

Diez B. Roggisch

creates a syntax error. Why can't a class have
a method called "print"? Doesn't seem to be
any way it could be confused with the reserved
print statement, or am I missing something?

Obviously, otherwise python wouldn't complain :)

As methods aren't much different from plain functions, I think we can agree
that a exception rule like "in methods, keywords can be used as names, but
not on normal functions" would be quite confusing.

Now consider this example:
<function foo at 0x4052287c>

Now what would you expect a

to do when print is issued alone on the commandline?

print as class method could be referenced as as klass.print of course, but
as I stated above, that would be inconsistent.

Another reason might be that creating a context-relevant re-interpretation
of keywords complicates the parser uneccesarily - with very limited
benefit.


-
Regards,

Diez B. Roggisch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top