Why I need the parameter when the call doesn't use it?
John Gordon
gordon at panix.com
Sun Aug 28 23:27:17 EDT 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
self.say_hello()
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
decorator.
--
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