[Tutor] **kw and self

Deirdre Saoirse Moen deirdre@deirdre.net
Wed, 20 Jun 2001 10:33:00 -0700


At 6:55 PM +0200 6/20/01, Remco Gerlich wrote:
>On  0, Willi Richert <w.richert@gmx.net> wrote:
>>  as a bloody newbie (who as reverted already some others to Python) I walked
>>  34 times through the whole web and did not get the real meaning of **kw and
>>  self.
>
>*breathe in deeply*
>
>Ok.
>
>PART I: THE HEDGEHOG
>--------------------
>
>No wait, that's wrong.

::snort:;

Thanks for making my morning. I'm tired and grumpy, but this made me laugh.

Therefore I'll answer the self question. :)

self, when used with variables, means: "for this instance of this 
class, this variable is set to that."

Most classes have multiple instances, meaning there's 52 cards or 7 
robots or whatever.

When self is used with methods, it means "use the function that 
belongs to this class."

Note that the first argument, implied when you pass something to a 
class's method, is self.

consider the following example:

class robot:
  def __init__(self, name, robotlist):
   self.name = name
   robotlist.append(self)

robotlist = []

robbie = robot('In',  robotlist)
robbie = robot('A',   robotlist)
robbie = robot('B',   robotlist)
robbie = robot('C',   robotlist)
robbie = robot('D',   robotlist)
robbie = robot('E',   robotlist)
robbie = robot('Out', robotlist)

for i in robotlist:
  print i.name

-- 
_Deirdre    Stash-o-Matic: http://weirdre.com    http://deirdre.net
"Cannot run out of time.... Is infinite time. You... are finite....
Zathrus... is finite. This... is wrong tool!" -- Zathrus