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

N

Niklas Rosencrantz

I modularize code for a webapp and I want to know what python makes that a need to define an argument called self? 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:

----
class A(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
def is_submitter_human(self):
cResponse = captcha.submit( self.request.get('recaptcha_challenge_field').encode('utf-8'), self.request.get('recaptcha_response_field').encode('utf-8'),
CAPTCHA_PRV_KEY,
os.environ['REMOTE_ADDR'])
return cResponse.is_valid

def post(self, view):
logging.debug('starting recaptcha check')
isHuman = self.is_submitter_human()# here I don't pass a parameter
logging.debug('recaptcha check isHuman:' +str(isHuman))
if not isHuman:#failed captcha and can try again
#Reprint the form

--
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 andpython requires me to add one parameter to the function head and I wonder if you call tell me something about the background why?

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

Thank you
 
C

Chris Gonnerman

I modularize code for a webapp and I want to know what python makes that a need to define an argument called self? 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:
is_submitter_human() isn't a function, it's a method. Methods are
always called with a reference to the class instance (i.e. the object)
that the method belongs to; this reference is the first argument, and is
conventionally called "self".

Though I've hacked it out, your code sample includes calls to other
methods of the object, by calling self.methodname(). Without the first
parameter, how else would you do it?

-- Chris.
 
C

Chris Rebert

I modularize code for a webapp and I want to know what python makes that a need to define an argument called self? 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:

----
class A(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
   def is_submitter_human(self):
       cResponse = captcha.submit(                     self.request.get('recaptcha_challenge_field').encode('utf-8'),                     self.request.get('recaptcha_response_field').encode('utf-8'),
                    CAPTCHA_PRV_KEY,
                    os.environ['REMOTE_ADDR'])
       return cResponse.is_valid

   def post(self, view):
       logging.debug('starting recaptcha check')
       isHuman = self.is_submitter_human()# here I don't pass a parameter
       logging.debug('recaptcha check isHuman:' +str(isHuman))
       if not isHuman:#failed captcha and can try again
           #Reprint the form

Some other languages name the analogous parameter "this" instead of
"self", and basically declare it implicitly for you. In both cases,
said variable is used to refer to the object that the current method
is being called on (e.g. if `w` is a list and I do w.append(v), the
list `w` is `self` in the context of the .append() method call). Since
Python's object-orientation is slightly impure, you are required to
declare `self` explicitly in the parameter list, unlike most other
languages. Technically, you are free to name the parameter something
else instead of "self"; naming it "self" is merely a convention.
However, no matter its name, the first parameter to a method will
always receive the current object as its argument value.

So, given: x = Foo()
Then: x.bar(y, z)
is approximately equivalent to:
Foo.bar(x, y, z) # perfectly legal working code

So the number of arguments actually /does/ match the number of
parameters; `x`/`self` is just passed implicitly via the semantics of
the dot (".") operator.

Cheers,
Chris
 
J

John Gordon

In said:
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.
 
S

Steven D'Aprano

No, that's not true and may lead to future confusion.

Rather, it is a function *and* a method. Not all functions are methods,
but all methods are functions.

Wouldn't it be more accurate to say that methods *wrap* functions?
.... def spam(self):
.... pass
....<function spam at 0xb7e8c25c>


(At least for pure Python methods... those written in C, such as for the
built-in types, don't.)

Also not true, but perhaps too subtle an issue to explore in this thread.

But for the record, you have "normal" instance methods, class methods,
static methods, and any other sort of method you can create using the
descriptor protocol, such as this one:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/

But as Ben hints at, this is getting into fairly advanced territory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top