[Tutor] Classes, methods and self

Bob Gailer bgailer@alum.rpi.edu
Fri Aug 1 12:25:05 2003


--=======54A65727=======
Content-Type: text/plain; x-avg-checked=avg-ok-6564133F; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit

At 10:18 AM 8/1/2003 -0500, Justin Heath wrote:

>I am going through some various class documentation (Learning to Program 
>and Learning Python) and I cannot seem to understand why self is needed in 
>a method declaration. I "believe" that I understand what self does (i.e. 
>connects itself to the current object instance). However, what I do not 
>understand is why you have to explicitly declare self in the argument list 
>of a method. I hope that was clear. Can anyone clarify this for me?

class A:
   def foo(self,arg):
     print arg

a = A()
a.foo(3)

Python converts a.foo(3) into A.foo(a, 3), thus passing the instance as the 
first argument to the class method foo.

Bob Gailer
bgailer@alum.rpi.edu
303 442 2625

--=======54A65727=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-6564133F
Content-Disposition: inline


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003

--=======54A65727=======--