in a class method self
is the instance of the class the method is called on. beware that self
is not a keyword in python just a conventional name given to the first argument of a method.
look at this example:
class A:
def foo(self):
print "I'm a.foo"
@staticmethod
def bar(s):
print s
a = A()
a.foo()
A.foo(a)
here a
is the instance of the class A
. calling a.foo()
you are invoking the method foo
of the instance a
while A.foo(a)
invoke the method foo
in the class A
but passing the instance a
as first argument and they are exactly the same thing (but never use the second form).
staticmethod
is a decorator that let you define a class method as static. that function is no more a method and the first argument is not the instance of the class but is exactly the first argument you passed at that function:
a.bar("i'm a static method")
i'm a static method
A.bar("i'm a static method too")
i'm a static method too
PS. i don't want to bothering you but these are the very basis of python, the python tutorial is a nice start for the beginners.