Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82509

Classes - converting external function to class's method.

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 <webmailgroups@gmail.com>
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" <webmailgroups@gmail.com>
To <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16949.1418574125.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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. 



Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Classes - converting external function to class's method. "Ivan Evstegneev" <webmailgroups@gmail.com> - 2014-12-14 18:20 +0200
  Re: Classes - converting external function to class's method. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-15 04:28 +1100
    RE: Classes - converting external function to class's method. "Ivan Evstegneev" <webmailgroups@gmail.com> - 2014-12-14 21:49 +0200
  Re: Classes - converting external function to class's method. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-12-15 00:15 +0100
    Re: Classes - converting external function to class's method. Terry Reedy <tjreedy@udel.edu> - 2014-12-14 19:11 -0500
      Re: Classes - converting external function to class's method. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-12-15 01:32 +0100
    Re: Classes - converting external function to class's method. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-15 13:01 +1100
      RE: Classes - converting external function to class's method. "Ivan Evstegneev" <webmailgroups@gmail.com> - 2014-12-15 21:13 +0200

csiph-web