Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'receives': 0.04; 'argument': 0.05; 'attribute': 0.07; 'class,': 0.07; 'explicit': 0.07; 'method.': 0.07; 'variables': 0.07; 'attributes': 0.09; 'function:': 0.09; 'here?': 0.09; 'method:': 0.09; 'methods,': 0.09; 'mind,': 0.09; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'wrote': 0.14; '"x"': 0.16; '(just': 0.16; '......': 0.16; 'argument,': 0.16; 'argument:': 0.16; 'attributes.': 0.16; 'attributes:': 0.16; 'downside': 0.16; 'instance:': 0.16; 'mapped': 0.16; 'notation': 0.16; 'positional': 0.16; 'skip:| 70': 0.16; 'subject:Classes': 0.16; 'subject:class': 0.16; 'typeerror:': 0.16; 'obviously': 0.18; 'bit': 0.19; 'module': 0.19; 'everyone,': 0.19; '>>>': 0.22; 'creating': 0.23; 'exists': 0.24; 'looks': 0.24; 'question': 0.24; "i've": 0.25; 'this:': 0.26; 'pass': 0.26; 'code:': 0.26; 'defined': 0.27; 'function': 0.29; 'external': 0.29; "doesn't": 0.30; 'code': 0.31; "skip:' 10": 0.31; '(maybe': 0.31; 'implied': 0.31; 'ok.': 0.31; 'omitted': 0.31; 'class': 0.32; 'checked': 0.32; 'text': 0.33; 'becomes': 0.33; 'checking': 0.33; 'guess': 0.33; 'moment': 0.34; 'skip:_ 10': 0.34; 'created': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'described': 0.36; 'like,': 0.36; 'right?': 0.36; 'skip:> 10': 0.36; 'done': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'list': 0.37; 'step': 0.37; 'message-id:@gmail.com': 0.38; 'initially': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:- 60': 0.39; 'either': 0.39; 'called': 0.40; 'according': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'eventually': 0.60; 'till': 0.61; 'new': 0.61; 'simple': 0.61; 'here:': 0.62; 'name': 0.63; 'such': 0.63; 'received:79': 0.64; 'become': 0.64; 'more': 0.64; 'note:': 0.66; 'here': 0.66; 'reads': 0.68; 'fact,': 0.69; 'applying': 0.72; 'now:': 0.74; '.....': 0.78; 'skip:* 70': 0.78; 'age': 0.80; "class's": 0.84; 'itself?': 0.84; '******': 0.91; 'hand,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:mime-version:content-type :content-transfer-encoding:thread-index:content-language; bh=xf1q58y0nQ6phDtOJEGhNMYs1rWsclIzS/yrB8HLbZc=; b=e+lhVDwZ+8V3D5eoceav3ujmxP9IlzzGApXmHjYzHSskhhsjEiyROVruqqD3gkzAHP k3DsopoTYX5pk/xkGy5I2cmJ23LWdUV2vnwijAs80dC18fzC+hCEKI2eZZ33p7z4qlHB 5u8mCN1HlUmlSU3yTwMWxtyi4l0lOs806ZRtw+MAUV8/zRXxG68I3eZKrwzSgkoyN9nw 1CmmYZ0rU22vZ9d/HWigCgOcBvK1yyHeIz3CkLvGxrqBfmfdyqW7lBKe+10RVLS+BXsV enXmZGUrR3VrhBnsj/vhqWkhlDW+7RF77vhcRfHNpxjF713nJicubjM5I2ffh4DgXAUn TLTw== X-Received: by 10.180.91.193 with SMTP id cg1mr24652616wib.26.1418574118491; Sun, 14 Dec 2014 08:21:58 -0800 (PST) From: "Ivan Evstegneev" To: Subject: Classes - converting external function to class's method. Date: Sun, 14 Dec 2014 18:20:48 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AdAXtbXL/0FE+U0nSlmJYXS/f/vE7w== Content-Language: en-us X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 174 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418574125 news.xs4all.nl 2896 [2001:888:2000:d::a6]:47679 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82509 Hello Everyone, I have stuck a bit with this example(took this one from the book). Here are a description steps of what I've done till now: Step 1 - creating an empty namespace: >>>class rec: pass Step 2 - adding some attributes: >>>rec.name = 'Bob' >>>rec.age = 40 Step 3 - Creating an instance: >>>x= rec() Step 4 - write down external function: >>>def uppername(obj): return obj.name.upper() Step 5 - applying this function on x: >>>uppername(x) 'BOB' Till this moment its Ok. But: Step 6 - Creating a class method: >>>rec.method = uppername Then using the "help" function I get (just checking what happened): >>>help(rec) class rec(builtins.object) | Methods defined here: | | method = uppername(obj) | | ---------------------------------------------------------------------- |omitted text...... |------------------------------------------------------------------------ | Data and other attributes defined here: | | age = 40 | | name = 'Bob' Then the same about x: >>>help(x) class rec(builtins.object) | Methods defined here: | | method = uppername(obj) | ------------------------------------------------------------------- omitted text ..... |--------------------------------------------------------------------- The last check I've made: >>> help(x.method) Help on method uppername in module __main__: uppername() method of __main__.rec instance >>> help(rec.method) Help on function uppername in module __main__: uppername(obj) and now to the Step 7 - using this new "method" on x: >>>x.method() 'BOB' >>>rec.method() ..... ..... TypeError: uppername() missing 1 required positional argument: 'obj' and then: >>>rec.method(rec) 'BOB' So my the question is: what is going on here? How this actually works? According to the book's downside note: **************************************************************************** ****** In fact, this is one of the reasons the self argument must always be explicit in Python methods-because methods can be created as simple functions independent of a class, they need to make the implied instance argument explicit. They can be called as either functions or methods, and Python can neither guess nor assume that a simple function might eventually become a class's method. The main reason for the explicit self argument, though, is to make the meanings of names more obvious: names not referenced through self are simple variables mapped to scopes, while names referenced through self with attribute notation are obviously instance attributes. **************************************************************************** ***** Keeping it in mind, I wrote this code: >>> class myclass: def __init__(self, value): self.name = value def myupper(self): return self.name.upper() And then: >>> b = myclass('bob') >>>b.myupper() 'BOB' I made an assumption that this is what happens(roughly speaking) when I write this: >>>rec.method = uppername Am I right? (close enough maybe?) On the other hand, when I checked further: >>>dir(x.method) [........, '__self__',.......] but >>>dir(rec.method) [.... list of method and '__self__' wasn't there.....] I got a "bit" confused. Why actually in instance "x" >>>x.method() can auto-assign an "x" to uppername() function argument, but it doesn't work in a class "rec" itself? So I need to write down an "obj" argument manually. Why '__self__' exists only in instance "x"? (maybe because "rec", initially was created as an empty class?) How does a function "uppername()" looks like, when I apply it to "rec" class, so it becomes a class's method? So in such a case, when instance "x" receives an "update" it reads the new method as I described above(I mean the code of "myclass")? Thanks a lot in advance, Ivan.