Why I need the parameter when the call doesn't use it?

John Gordon gordon at panix.com
Mon Aug 29 05:27:17 CEST 2011

In <66a3f64c-d35e-40c7-be69-ddf708e37ba7 at glegroupsg2000goo.googlegroups.com> Niklas Rosencrantz <niklasro at gmail.com> writes:

> What's the story of using these parameters that are called "self"?

"self" is a reference to the class object, and it allows the method to
access other methods and variables within the class.

For example, say you have this class:

  class MyClass(object):

    def method1(self, x):
      self.x = x

    def say_hello(self):
      self.x = self.x + 1
      print "hello"

Without the "self" reference, method1 wouldn't be able to access
instance variable x and it wouldn't be able to call say_hello().

If you have a method that doesn't need to access other variables or
methods within the class, you can declare it with the @staticmethod

John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

More information about the Python-list mailing list