PEP 8 example of 'Function and method arguments'

Discussion in 'Python' started by Martin P. Hellwig, Mar 13, 2006.

  1. 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
    Martin P. Hellwig, Mar 13, 2006
    #1
    1. Advertising

  2. 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
    Steven Bethard, Mar 13, 2006
    #2
    1. Advertising

  3. 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 ''.split('@')])"
    bruno at modulix, Mar 13, 2006
    #3
  4. On Mon, 13 Mar 2006 17:18:16 +0100
    "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?


    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 ()
    Anansi Spaceworks http://www.AnansiSpaceworks.com
    Terry Hancock, Mar 14, 2006
    #4
  5. Martin P. Hellwig

    Duncan Booth Guest

    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.
    Duncan Booth, Mar 14, 2006
    #5
  6. <cut>
    Steven, Bruno, Terry & Duncon, thank you for your insights, it really
    helped me a great deal.

    --
    mph
    Martin P. Hellwig, Mar 14, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Ehrenberg
    Replies:
    3
    Views:
    319
    Daniel Ehrenberg
    Jan 7, 2004
  2. Robert Brewer
    Replies:
    3
    Views:
    274
    Samuel Walters
    Jan 8, 2004
  3. Christoph Becker-Freyseng

    PEP for new modules (I read PEP 2)

    Christoph Becker-Freyseng, Jan 15, 2004, in forum: Python
    Replies:
    3
    Views:
    360
    Gerrit Holl
    Jan 16, 2004
  4. jmborr
    Replies:
    1
    Views:
    397
    Stargaming
    Nov 3, 2007
  5. Lie
    Replies:
    25
    Views:
    717
    Dafydd Hughes
    Dec 18, 2007
Loading...

Share This Page