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


Groups > comp.lang.python > #76177

odd difference calling function from class or instance variable

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!news.unit0.net!cyclone03.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!post02.fr7!fx12.am4.POSTED!not-for-mail
From GregS <not@my.real.address.com>
Newsgroups comp.lang.python
Message-ID <201408131006549222-not@myrealaddresscom> (permalink)
MIME-Version 1.0
Content-Type text/plain; charset=iso-8859-1; format=flowed
Content-Transfer-Encoding 8bit
Subject odd difference calling function from class or instance variable
User-Agent Unison/2.1.10
Lines 38
NNTP-Posting-Host 82.26.7.234
X-Complaints-To http://netreport.virginmedia.com
X-Trace 1407920815 82.26.7.234 (Wed, 13 Aug 2014 09:06:55 UTC)
NNTP-Posting-Date Wed, 13 Aug 2014 09:06:55 UTC
Organization virginmedia.com
Date Wed, 13 Aug 2014 10:06:54 +0100
X-Received-Body-CRC 4213757942
X-Received-Bytes 1805
Xref csiph.com comp.lang.python:76177

Show key headers only | View raw


Hello,

This is my first post here so please gently inform me of any etiquette 
breaches.

I'm seeing a behaviour I can't explain with Python 3.4.1 when I call a 
function via a reference stored in an object.

When I assign the reference as a class variable, the reference has 
__self__ set, too, so I get an extra argument passed to the function.  
If I assign the reference as an instance variable, then __self__ is 
unset so no extra argument.

Here's what I mean:

>>> def print_args(*args):
	print(args)
	
>>> class C:
	ref = None
	
>>> C.ref = print_args    # assign to class variable
>>> i = C()
>>> i.ref()     # call via class variable - get a 'self' argument passed
(<__main__.C object at 0x1071a05f8>,)
>>> i.ref = print_args   # assign to instance variable
>>> i.ref()     # call via instance variable: no arguments
()

If you look at i.ref.__self__ for the two cases, you'll see what's 
going on.  I've tried RTFMing but can't find the reason for the two 
behaviours.  Could someone provide an explanation for me, please?

Thanks,

Greg

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


Thread

odd difference calling function from class or instance variable GregS <not@my.real.address.com> - 2014-08-13 10:06 +0100
  Re: odd difference calling function from class or instance variable Peter Otten <__peter__@web.de> - 2014-08-13 11:40 +0200
  Re: odd difference calling function from class or instance variable Chris Angelico <rosuav@gmail.com> - 2014-08-13 19:45 +1000
  Re: odd difference calling function from class or instance variable GregS <not@my.real.address.com> - 2014-08-13 11:20 +0100
    Re: odd difference calling function from class or instance variable Chris Angelico <rosuav@gmail.com> - 2014-08-13 20:29 +1000

csiph-web