self argument

Moshe Zadka moshez at math.huji.ac.il
Sat Feb 26 05:45:11 EST 2000


On Sat, 26 Feb 2000 london999 at my-deja.com wrote:

> I am very new to Python, but it appears that every function defined in a
> class must have self as a first variable name. 

No, it can call the argument what it likes. "self" is the most popular,
by far. "me", "my" and "s" are poor contestants most people don't even
know about. But it's a convention, not a rule.

> Assuming that is the
> case, it would be nice if there was an error message given when the
> interpreter encounters a function that is defined without self (rather
> than giving the error message "TypeError: no arguments expected" when
> the function is called. 

It can't. Consider:

class foo:
	pass

def bar():
	pass

foo.bar = bar

a = foo()
a.bar()

The only time the interpreter can give the error is when a.bar() is
called, and that's when it does.

> Even better, it would be nice (probably too late
> I know), if self was defined implicitly as the "this" pointer is in C++.

That would add another keyword to the language, and contradict one of
Python's principles "explicit is better then implicit"
--
Moshe Zadka <mzadka at geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.





More information about the Python-list mailing list