Why the 'self' argument?

Grzegorz Staniak gstaniak at inka.zagiel.pl
Thu Sep 11 13:33:38 CEST 2003


John Roth writes:

>> I'm a newbie Python user, a systems administrator - I've been trying
>> to switch from Perl to Python for administrative tasks - and one thing
>> I cannot understand so far is why I need the special 'self' (or anything
>> esle) argument in class method definitions. I might have missed an
>> explanation in the docs, a quick Google search did not help. Is there
>> somewhere on the web you could direct me to?
> 
> It's in the FAQ somewhere.
> 
> Technically, it would be possible to make "self" a reserved
> word, and not have to put it in the method declaration. However,
> there are a lot of people who use something other than the
> word "self," so that would break existing code.
> 
> John Roth

I'd like to thank you and all those who answered here and in private
e-mails. It wasn't my intention to start a 50-postings debate ;)

To sum up what I've learnt here and from the FAQ: 'self' could actually be 
any string (it is 'self' by convention), as its role is only to serve as 
the first argument of methods in order to facilitate the process of
associating method calls with their context. It would be possible (even if
not obviously desirable) to implement method definitions in such a way
as to avoid the first argument completely, i.e.

	def something():
		...

so actually it's just a question of implementation. Having little
experience with object-oriented programming, I won't try to relate to
pro and contra arguments that have been given in the discussion, I just
have one more question: isn't it a little superfulous to have to use the
'classmethod()' builtin to declare class methods? I'd think giving a
class object name as the first argument should suffice?

Thanks,

-- 
Grzegorz Staniak <gstaniak at zagiel.com.pl>




More information about the Python-list mailing list