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


Groups > comp.lang.python > #12531

Re: Why do class methods always need 'self' as the first parameter?

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!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '3.2': 0.05; 'attributes': 0.05; 'instance': 0.05; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; '>>>>': 0.09; 'convention,': 0.09; 'parameter.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wrong,': 0.09; 'def': 0.15; 'argument': 0.15; 'case.': 0.15; 'class,': 0.15; 'method.': 0.15; "subject:' ": 0.15; '(is': 0.16; 'did.': 0.16; 'reedy': 0.16; 'translates': 0.16; 'val': 0.16; 'wrote:': 0.16; 'wrap': 0.18; 'jan': 0.19; 'seems': 0.20; 'header:In-Reply- To:1': 0.22; 'dictionary': 0.23; 'mask': 0.23; 'pm,': 0.24; 'code.': 0.26; 'function': 0.27; 'somebody': 0.28; 'subject:need': 0.28; 'bit': 0.28; 'bound': 0.29; 'print': 0.29; 'example': 0.30; 'confused': 0.30; 'typeerror:': 0.30; 'class': 0.30; 'equivalent': 0.31; 'subject:?': 0.31; 'list': 0.32; 'there': 0.33; 'to:addr :python-list': 0.33; 'someone': 0.34; 'header:User-Agent:1': 0.34; 'right,': 0.34; 'header:X-Complaints-To:1': 0.35; 'object': 0.35; 'explain': 0.36; 'but': 0.37; 'received:org': 0.38; 'some': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'else': 0.39; 'to:addr:python.org': 0.39; 'hope': 0.61; 'double': 0.61; 'special': 0.67; 'why?': 0.73; 'self.value': 0.84; 'subject:always': 0.84; 'subject:class': 0.84; 'subject:self': 0.84; 'terminology': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Why do class methods always need 'self' as the first parameter?
Date Wed, 31 Aug 2011 18:40:26 -0400
References <0dc26f12-2541-4d41-8678-4fa53f347acf@g9g2000yqb.googlegroups.com> <j3lnlf$8eu$1@dough.gmane.org> <0604E20B5F6F2F4784C9C8C71C5DD4DD2F163CD332@EMARC112VS01.exchad.jpmchase.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0
In-Reply-To <0604E20B5F6F2F4784C9C8C71C5DD4DD2F163CD332@EMARC112VS01.exchad.jpmchase.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.635.1314830484.27778.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314830484 news.xs4all.nl 2494 [2001:888:2000:d::a6]:43543
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12531

Show key headers only | View raw


On 8/31/2011 1:12 PM, Prasad, Ramit wrote:
>> def double(obj): return 2*obj.value
>>
>> class C:
>>   def __init__(self, val):
 >>       self.value = val
>>
>> c = C(3)
 >> C.double = double
 >> c.doub = double
 >> # not c.double as that would mask access to C.double in c.double()
 >> print(double(c),
>> C.double(c), c.double(), c.doub(c))

Above is 3.2 code. To be exactly equivalent with 2.x, you need
class C(object):

> Sorry if I get some of the following terminology wrong, I get a bit
> confused on Python terms. I hope the following is still coherent. (Is
> there a dictionary of Python terminology?)

> Given the above example I get this
>>>> print c.double(c)
> TypeError: double() takes exactly 1 argument (2 given)

Right, because c.double() translates to C.double(c), and c.double(x)
translates to C.double(c,x), which is not valid.

>>>> print c.doub(c)
> 6
>
> It seems to me that if I add a function to the list of class
> attributes it will automatically wrap with "self"

When accessed via an instance of the class, the instance is 
automagically added as the first argument to be bound to the first 
parameter. The name 'self' is a convention, not a requirement.

> but adding it to
> the object directly will not wrap the function as a method. Can
> somebody explain why?

Someone else did. Not wrapping is normal, wrapping is a special case.

-- 
Terry Jan Reedy

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


Thread

Why do class methods always need 'self' as the first parameter? "T. Goodchild" <tgoodchild@gmail.com> - 2011-08-31 07:35 -0700
  Re: Why do class methods always need 'self' as the first parameter? John Gordon <gordon@panix.com> - 2011-08-31 14:54 +0000
    Re: Why do class methods always need 'self' as the first parameter? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-01 02:06 +1000
      Re: Why do class methods always need 'self' as the first parameter? Grant Edwards <invalid@invalid.invalid> - 2011-08-31 16:20 +0000
  Re: Why do class methods always need 'self' as the first parameter? Emile van Sebille <emile@fenx.com> - 2011-08-31 08:01 -0700
  Re: Why do class methods always need 'self' as the first parameter? Neil Cerutti <neilc@norwich.edu> - 2011-08-31 15:05 +0000
  Re: Why do class methods always need 'self' as the first parameter? Javier Collado <javier.collado@gmail.com> - 2011-08-31 17:10 +0200
  Re: Why do class methods always need 'self' as the first parameter? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-01 01:41 +1000
    Re: Why do class methods always need 'self' as the first parameter? Chris Torek <nospam@torek.net> - 2011-08-31 20:15 +0000
      Re: Why do class methods always need 'self' as the first parameter? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-01 10:48 +1000
        Re: Why do class methods always need 'self' as the first parameter? Chris Angelico <rosuav@gmail.com> - 2011-09-01 11:47 +1000
        Re: Why do class methods always need 'self' as the first parameter? Eric Snow <ericsnowcurrently@gmail.com> - 2011-08-31 20:07 -0600
        Re: Why do class methods always need 'self' as the first parameter? Chris Torek <nospam@torek.net> - 2011-09-01 02:20 +0000
      Re: Why do class methods always need 'self' as the first parameter? Piet van Oostrum <piet@vanoostrum.org> - 2011-09-05 16:15 +0200
        Re: Why do class methods always need 'self' as the first parameter? Chris Torek <nospam@torek.net> - 2011-09-06 01:10 +0000
          Re: Why do class methods always need 'self' as the first parameter? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-06 14:31 +1000
  Re: Why do class methods always need 'self' as the first parameter? Terry Reedy <tjreedy@udel.edu> - 2011-08-31 12:30 -0400
  RE: Why do class methods always need 'self' as the first parameter? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2011-08-31 13:12 -0400
    Re: Why do class methods always need 'self' as the first parameter? Piet van Oostrum <piet@vanoostrum.org> - 2011-09-08 01:01 +0200
  Re: Why do class methods always need 'self' as the first parameter? Chris Rebert <clp2@rebertia.com> - 2011-08-31 11:31 -0700
  Re: Why do class methods always need 'self' as the first parameter? Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-31 15:52 -0600
  Re: Why do class methods always need 'self' as the first parameter? Terry Reedy <tjreedy@udel.edu> - 2011-08-31 18:40 -0400
  Re: Why do class methods always need 'self' as the first parameter? UncleLaz <andrei.lisnic@gmail.com> - 2011-09-01 03:40 -0700
    Re: Why do class methods always need 'self' as the first parameter? Michiel Overtoom <motoom@xs4all.nl> - 2011-09-01 13:23 +0200
  Re: Why do class methods always need 'self' as the first parameter? John Roth <johnroth1@gmail.com> - 2011-09-01 05:45 -0700
    Re: Why do class methods always need 'self' as the first parameter? Ian Kelly <ian.g.kelly@gmail.com> - 2011-09-01 08:26 -0600
      Re: Why do class methods always need 'self' as the first parameter? John Roth <johnroth1@gmail.com> - 2011-09-02 10:51 -0700
        Re: Why do class methods always need 'self' as the first parameter? Ian Kelly <ian.g.kelly@gmail.com> - 2011-09-02 14:30 -0600
          Re: Why do class methods always need 'self' as the first parameter? John Roth <johnroth1@gmail.com> - 2011-09-02 15:30 -0700
  Re: Why do class methods always need 'self' as the first parameter? rantingrick <rantingrick@gmail.com> - 2011-09-05 20:58 -0700

csiph-web