[Tutor] Methods and classes

Kent Johnson kent37 at tds.net
Thu Sep 14 01:00:50 CEST 2006


Chris Hengge wrote:
> On Wed, 2006-09-13 at 18:26 -0400, Kent Johnson wrote:

>> You have to distinguish between a method (a function that is part of a 
>> class definition) and a standalone function (not part of any class). 
>> Python allows both. Standalone functions don't have a 'self' parameter; 
>> class methods always do (you can give it a different name but if you 
>> omit it you will get a runtime error when you call the method).
>>
>> Kent
>>
> So just make sure I always declare self for methods (functions in
> classes)? Is this unique to python? or do some other languages already
> include self, and just hide it from the programmer? 

All the OO languages I know have a similar concept. Python is more 
explicit than most. Java has a 'this' variable that is magically defined 
in the scope of any method. For most accesses to member variables and 
methods you don't even have to specify 'this' - the language figures it 
out for you. Ruby uses a naming convention to refer to attributes inside 
a method (attribute names start with @)

One of the guiding principles of Python is "explicit is better than 
implicit" and the explicit declaration and use of 'self' is consistent 
with this.

Kent



More information about the Tutor mailing list