Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12383
| From | John Gordon <gordon@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Why I need the parameter when the call doesn't use it? |
| Date | 2011-08-29 03:27 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <j3f0ul$69h$1@reader1.panix.com> (permalink) |
| References | <66a3f64c-d35e-40c7-be69-ddf708e37ba7@glegroupsg2000goo.googlegroups.com> |
In <66a3f64c-d35e-40c7-be69-ddf708e37ba7@glegroupsg2000goo.googlegroups.com> Niklas Rosencrantz <niklasro@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@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why I need the parameter when the call doesn't use it? Niklas Rosencrantz <niklasro@gmail.com> - 2011-08-28 17:26 -0700
Re: Why I need the parameter when the call doesn't use it? Ben Finney <ben+python@benfinney.id.au> - 2011-08-29 11:32 +1000
Re: [Python] Why I need the parameter when the call doesn't use it? Chris Gonnerman <chris@gonnerman.org> - 2011-08-28 20:42 -0500
Re: [Python] Why I need the parameter when the call doesn't use it? Ben Finney <ben+python@benfinney.id.au> - 2011-08-29 12:34 +1000
Re: [Python] Why I need the parameter when the call doesn't use it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-29 14:40 +1000
Re: Why I need the parameter when the call doesn't use it? Chris Rebert <clp2@rebertia.com> - 2011-08-28 18:52 -0700
Re: Why I need the parameter when the call doesn't use it? John Gordon <gordon@panix.com> - 2011-08-29 03:27 +0000
Re: Why I need the parameter when the call doesn't use it? Ben Finney <ben+python@benfinney.id.au> - 2011-08-29 16:15 +1000
csiph-web