PEP 8 example of 'Function and method arguments'

Terry Hancock hancock at anansispaceworks.com
Mon Mar 13 21:37:34 EST 2006


On Mon, 13 Mar 2006 17:18:16 +0100
"Martin P. Hellwig" <mhellwig at xs4all.nl> wrote:

> While I was reading PEP 8 I came across this part:
> 
> """
> Function and method arguments
>     Always use 'self' for the first argument to instance
>     methods. Always use 'cls' for the first argument to
>     class methods.
> """
> 
> Now I'm rather new to programming and unfamiliar to some
> basic concepts  of OOP. However I wrote most of my classes
> in the new style way and by  this I have always used
> 'self' for the first argument of an Instance  method, but
> now I'm unsure what actually the difference is between an 
> instance and a class method is and when to use it in which
> case.
> 
> Could somebody please enlighten me (a rtfm/wrong newsgroup
> is just as  welcome of course), preferably in a short code
> example?

In short, if you don't know what it is, you don't need it.
;-)

So far, the only time I've ever encountered this is with the
__new__ method, which, being a classmethod, needs "cls"
(which gets loaded with the *class* not the *instance*).

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list