Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PEP 8 example of 'Function and method arguments'

Reply
Thread Tools

PEP 8 example of 'Function and method arguments'

 
 
Martin P. Hellwig
Guest
Posts: n/a
 
      03-13-2006
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?

TIA

--
mph
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      03-13-2006
Martin P. Hellwig 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?


You're probably doing fine.

class C(object):

# instance method
def foo(self):
...

# class method
@classmethod
def bar(cls):
...

It's probably pretty unlikely that you've declared any class methods,
but if you have, you should be able to identify them by the call to
``classmethod``. If you don't see any of those, you're fine.

A class method is just a method that can be called like:

Class.method()

instead of having to be called like:

instance.method()

For 99% of the methods you write, I'd expect them to be instance methods.

STeVe
 
Reply With Quote
 
 
 
 
bruno at modulix
Guest
Posts: n/a
 
      03-13-2006
Martin P. Hellwig 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.


As you noticed, an instance method gets an instance as the first
parameter (usually named 'self'), and does something with this instance.
A class method doesn't takes the instance as first parameter, but the
class itself, and does something with the class (NB: in Python, classes
are objects too...).

Note that, as well as class methods, there are class variables (see
below for an example).

> Could somebody please enlighten me (a rtfm/wrong newsgroup is just as
> welcome of course),


No problem, you're at the right place.

> preferably in a short code example?


A stupid exemple that won't teach you much about the usefulness of class
methods :

class Foo(object):
# a class variable:
_number_of_foos = 0

# a class method
@classmethod
def increase_foo_counter(cls):
cls._number_of_foos +=1

# another class method
@classmethod
def decrease_foo_counter(cls):
cls._number_of_foos -=1

# and a third one
@classmethod
def get_foos_count(cls):
return cls._number_of_foos

# instance methods
def __init__(self):
self.increase_foo_counter()
print "now we have %d foos" % self.get_foos_count()

def __del__(self):
self.decrease_foo_counter()
print "now we have %d foos" % self.get_foos_count()

foos = [Foo() for i in range(10)]
print "how many foos ? %d " % Foo.get_foos_count()
del foos


Note that a class method can be called on the class itself or on any of
it's instances - it'll still get the class as first param.


FWIW, you won't probably need class methods before you get much more
familiar with OO. Not that they are an advanced topic by themselves, but
their usefulness appears mostly in heavyly OO designs.

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Terry Hancock
Guest
Posts: n/a
 
      03-14-2006
On Mon, 13 Mar 2006 17:18:16 +0100
"Martin P. Hellwig" <(E-Mail Removed)> 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 ((E-Mail Removed))
Anansi Spaceworks http://www.AnansiSpaceworks.com

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      03-14-2006
Terry Hancock wrote:

> 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*).
>


The __new__ method isn't actually a classmethod. In
http://www.python.org/2.2.3/descrintro.html, GvR wrote:

> Factoid: __new__ is a static method, not a class method. I initially
> thought it would have to be a class method, and that's why I added the
> classmethod primitive. Unfortunately, with class methods, upcalls
> don't work right in this case, so I had to make it a static method
> with an explicit class as its first argument. Ironically, there are
> now no known uses for class methods in the Python distribution (other
> than in the test suite). However, class methods are still useful in
> other places, for example, to program inheritable alternate
> constructors.


 
Reply With Quote
 
Martin P. Hellwig
Guest
Posts: n/a
 
      03-14-2006
<cut>
Steven, Bruno, Terry & Duncon, thank you for your insights, it really
helped me a great deal.

--
mph
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PEP: add __sum__ method dmitrey Python 1 01-22-2012 12:08 PM
About Rational Number (PEP 239/PEP 240) Lie Python 25 12-18-2007 06:13 AM
PEP thought experiment: Unix style exec for function/method calls Michael Python 4 07-02-2006 03:24 PM
PEP 322 - alternate method names AC Python 0 08-26-2004 04:27 PM
PEP for new modules (I read PEP 2) Christoph Becker-Freyseng Python 3 01-16-2004 04:26 PM



Advertisments