Why do class methods always need 'self' as the first parameter?

John Gordon gordon at panix.com
Wed Aug 31 10:54:25 EDT 2011


In <0dc26f12-2541-4d41-8678-4fa53f347acf at g9g2000yqb.googlegroups.com> "T. Goodchild" <tgoodchild at gmail.com> writes:

> So why is 'self' necessary on class methods?  It seems to me that the
> most common practice is that class methods *almost always* operate on
> the instance that called them.  It would make more sense to me if this
> was assumed by default, and for "static" methods (methods that are
> part of a class, but never associated with a specific instance) to be
> labelled instead.

> Just curious about the rationale behind this part of the language.

How would a method access instance variables without 'self'?

They probably could have made 'self' a magical attribute that just
appears out of thin air instead of being passed as an argument, like
'this' in C++.  But would that really provide any benefit?

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list