PEP 8 example of 'Function and method arguments'

M

Martin P. Hellwig

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
 
S

Steven Bethard

Martin said:
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
 
B

bruno at modulix

Martin said:
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
 
T

Terry Hancock

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

Duncan Booth

Terry said:
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
 
M

Martin P. Hellwig

<cut>
Steven, Bruno, Terry & Duncon, thank you for your insights, it really
helped me a great deal.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top