Use empty string for self

Grant Edwards grante at visi.com
Wed Mar 1 17:14:32 CET 2006


On 2006-03-01, John Salerno <johnjsal at NOSPAMgmail.com> wrote:

> I do get it. I think I will just have to get used to seeing
> the 'self' argument but understanding that it's not really
> something that is always passed in.

But it _is_ always passed to the function.  You can even pass
it explicity when you call the method if you want:

  #!/usr/bin/python
  
  class MyClass:
      def mymethod(self,p1,p2):
          print self,p1,p2
        
  instance = MyClass()

  MyClass.mymethod(instance,1,2)

  instance.mymethod(1,2)

The two calls are equivalent.  

> I'm trying to train myself to see
>
> def doittoit(self) as def doittoit()

You would be misleading yourself.

> Of course, that might not be a good strategy, because I know
> when it isn't used as an instance method (is that C
> terminology?), then you must explicitly pass the self
> argument.

Exactly.

-- 
Grant Edwards                   grante             Yow!  I guess we can live
                                  at               on his POT FARM in HADES!!
                               visi.com            



More information about the Python-list mailing list