"self" - a python wart or "feature"

Bengt Richter bokr at oz.net
Mon Feb 17 01:35:02 EST 2003


On 16 Feb 2003 20:04:20 -0800, ceremona2000 at yahoo.com (Cere Davis) wrote:

>Hi Pythoners,
>
>I don't understand why Python requires "self" as the first argument in
>every function that intends to act on a instance of a given class eg:
>
>class C:
>    def stuff(self,x):
>        print x
>
>
>isn't it enough to assume that all function indented under class C
>will be acting on an instance of the object C unless they are
>specifily declared as staticmethods, etc?  Why bother requiring the
>self keyword in every stinkin' method?  This seems klunky to me but
>perhaps there is something I am missing here.
>
In your example, you don't explicitly use self, so it seems useless.
Try writing an example that sets instance attributes and uses class
variables and also makes use of temporary local variables in a method.

If you need more to think about, try passing a bound (to an instance)
method as an executable to something like map.

And then ask you question again, or propose some workable alternatives ;-)
Sorry to be a little lazy, but I'm not feeling like doing examples just now ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list