Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'context': 0.04; 'parameter': 0.05; 'convention.': 0.07; 'subject:when': 0.07; 'python': 0.08; 'argument': 0.15; 'subject:use': 0.15; 'given:': 0.16; 'naming': 0.16; 'operator.': 0.16; 'then:': 0.16; 'webapp': 0.16; '\xc2\xa0if': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'arguments': 0.18; 'slightly': 0.19; 'programming': 0.20; 'cc:no real name:2**0': 0.20; 'seems': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'wonder': 0.23; 'received:209.85.213.46': 0.23; 'received:mail- yw0-f46.google.com': 0.23; 'pm,': 0.24; 'variable': 0.24; 'aug': 0.24; "python's": 0.24; 'code': 0.25; 'parameters': 0.25; "i'm": 0.27; 'function': 0.27; 'subject:need': 0.28; 'pass': 0.29; 'explicitly': 0.29; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'match': 0.30; 'implicitly': 0.30; 'semantics': 0.30; 'skip:\xc2 20': 0.30; 'sun,': 0.30; 'class': 0.30; '(e.g.': 0.31; 'equivalent': 0.31; 'subject:?': 0.31; 'chris': 0.32; 'list': 0.32; 'value.': 0.32; '----': 0.32; "what's": 0.33; 'actually': 0.33; 'instead': 0.33; 'however,': 0.34; 'skip:b 40': 0.34; 'test': 0.34; 'languages.': 0.34; 'object': 0.35; 'languages': 0.37; 'passed': 0.37; 'using': 0.37; 'list,': 0.37; 'something': 0.37; 'cases,': 0.38; 'some': 0.38; 'received:google.com': 0.38; '8bit%:8': 0.38; 'received:209.85': 0.38; 'skip:o 20': 0.38; 'subject:: ': 0.39; 'unlike': 0.39; 'else': 0.39; 'define': 0.39; 'called': 0.40; 'background': 0.40; 'where': 0.40; 'you.': 0.62; 'free': 0.63; 'skip:+ 10': 0.64; 'here': 0.65; 'receive': 0.67; 'legal': 0.70; 'why?': 0.73; 'captcha': 0.84; 'sender:addr:chris': 0.84; 'subject:call': 0.91; 'to:none': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:cc:content-type :content-transfer-encoding; bh=9A9/trGE0PzKSfkTtYhbAAiUgS6tEAVGgmCdzzL7UGo=; b=dGGeZDzzrfRDqKymk5gybZF9An2++V7dywWX0Bciw/u2DNKRKCSuic8WpNiE/YYEf2 wc4T8bmCRV5+eKG73pFZnNugXfRDQ6hYjoohX8bmGXS+0eWRj9nJIJRdg1cfg2utNZpU unhK76sFLOipFfT6NIqEMHZ7olO9P4LaVFdFI= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <66a3f64c-d35e-40c7-be69-ddf708e37ba7@glegroupsg2000goo.googlegroups.com> References: <66a3f64c-d35e-40c7-be69-ddf708e37ba7@glegroupsg2000goo.googlegroups.com> Date: Sun, 28 Aug 2011 18:52:03 -0700 X-Google-Sender-Auth: ejZRZsB6DZUdCc3H2nmNuNefs8s Subject: Re: Why I need the parameter when the call doesn't use it? From: Chris Rebert Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314582726 news.xs4all.nl 2444 [2001:888:2000:d::a6]:51862 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12372 On Sun, Aug 28, 2011 at 5:26 PM, Niklas Rosencrantz wr= ote: > 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 modula= rizing a recaptcha test to a function and the I must add the parameter "sel= f" to the function is_submitter_human: > > ---- > class A(BaseHandler, blobstore_handlers.BlobstoreUploadHandler): > =C2=A0 =C2=A0def is_submitter_human(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0cResponse =3D captcha.submit( =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.request.get('r= ecaptcha_challenge_field').encode('utf-8'), =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.request.get('recaptcha_response= _field').encode('utf-8'), > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 CAP= TCHA_PRV_KEY, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 os.= environ['REMOTE_ADDR']) > =C2=A0 =C2=A0 =C2=A0 =C2=A0return cResponse.is_valid > > =C2=A0 =C2=A0def post(self, view): > =C2=A0 =C2=A0 =C2=A0 =C2=A0logging.debug('starting recaptcha check') > =C2=A0 =C2=A0 =C2=A0 =C2=A0isHuman =3D self.is_submitter_human()# here I = don't pass a parameter > =C2=A0 =C2=A0 =C2=A0 =C2=A0logging.debug('recaptcha check isHuman:' +str(= isHuman)) > =C2=A0 =C2=A0 =C2=A0 =C2=A0if not isHuman:#failed captcha and can try aga= in > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#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 a= nd python requires me to add one parameter to the function head and I wonde= r if you call tell me something about the background why? > > What's the story of using these parameters that are called "self"? 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 =3D 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