Why I need the parameter when the call doesn't use it?
Ben Finney
ben+python at benfinney.id.au
Sun Aug 28 21:32:22 EDT 2011
Niklas Rosencrantz <niklasro at gmail.com> writes:
> I modularize code for a webapp and I want to know what python makes
> that a need to define an argument called self?
Because, when calling a method on an instance, the instance is a
parameter to the call. That is,
foo = Thribble()
foo.bar("spam")
is usually syntactic sugar for
foo = Thribble()
foo.__class__.bar(foo, "spam")
and so the definition of that function on the Thribble class needs to
accept both parameters.
> Here's some code where I'm modularizing a recaptcha test to a function
> and the I must add the parameter "self" to the function
> is_submitter_human
Yes, because the code of ‘is_submitter_human’ needs to know *which*
instance has been passed. That instance is bound to the first parameter,
which is conventionally named ‘self’.
> It seems unlike other programming languages where the number of
> arguments in the call are the same as the number of arguments in the
> function head and python requires me to add one parameter to the
> function head and I wonder if you call tell me something about the
> background why?
I hope that explains.
See also:
<URL:http://docs.python.org/faq/programming.html#what-is-self>
<URL:http://docs.python.org/faq/design.html#why-self>
--
\ “Nullius in verba” (“Take no-one's word for it”) —motto of the |
`\ Royal Society, since 1663-06-30 |
_o__) |
Ben Finney
More information about the Python-list
mailing list