Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'argument': 0.05; 'assign': 0.07; '3.0,': 0.09; 'attributes': 0.09; 'classes.': 0.09; 'consistency': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:instance': 0.09; 'variable,': 0.09; 'jan': 0.12; "'__doc__',": 0.16; '.next': 0.16; '__lt__': 0.16; 'argument.': 0.16; 'attributes.': 0.16; 'conflicting': 0.16; 'func': 0.16; 'iterator': 0.16; 'old-style': 0.16; 'ought': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'set,': 0.16; 'subject: \n ': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '(the': 0.22; '>>>': 0.22; '(in': 0.22; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'documented': 0.24; 'equivalent': 0.26; 'possibly': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'went': 0.31; 'code': 0.31; "skip:' 10": 0.31; '13,': 0.31; 'class': 0.32; 'url:python': 0.33; 'alone': 0.33; 'becomes': 0.33; 'sense': 0.34; 'subject:from': 0.34; 'subject: (': 0.35; 'classes': 0.35; 'something': 0.35; 'objects': 0.35; 'add': 0.35; 'really': 0.36; 'done': 0.36; 'method': 0.36; 'url:org': 0.36; 'sometimes': 0.38; 'needed': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'received:71': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'users': 0.40; 'url:3': 0.61; 'first': 0.61; 'back': 0.62; 'such': 0.63; 'became': 0.64; 'between': 0.67; 'to,': 0.72; 'etc,': 0.84; 'gulf': 0.84; 'received:fios.verizon.net': 0.84; 'significance': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; 'on?': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Arbitrary dunder attributes (was Re: odd difference calling function from class or instance variable) Date: Wed, 13 Aug 2014 15:15:14 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-71-175-90-87.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407957354 news.xs4all.nl 2829 [2001:888:2000:d::a6]:48189 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76226 On 8/13/2014 5:51 AM, Chris Angelico wrote: > On Wed, Aug 13, 2014 at 7:06 PM, GregS wrote: >> 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. > > Spin-off from Greg's thread. > > The bound method object stores a reference to the original object (the > thing that becomes the first argument to the target function) in > __self__ (and the function in __func__). ISTM this ought to be _self > (and _func), as it's intended to be private; is it really something > that has language-level significance on par with __lt__ and so on? In 3.0, the iterator protocol .next became .__next__ for consistency with other syntax dunder methods. In 2.x, bound python-coded functions had im_func and im_self (and im_class for the class im_func was attached to, which has no equivalent in 3.x). Bound C-coded functions, such as [].sort, had and still have __self__ instead of im_self (and no equivalent of im_func/__func__). I presume this difference goes back to the gulf between C-coded types and Python-coded old-style classes. With old-style classes gone, im_class went and it made sense to consistently use __self__ instead of sometimes im_self (and change im_func to __func__ alone with it). In 3.0, function attributes were also dunderized. This was needed once function namespaces were unfrozen and users allowed to add possibly conflicting function attributes. >>> dir(lambda:0) #2.7 ['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] (In 2.7, many of the func attributes are duplicates: __code__ == func_code, etc, though this might not have always been true.) >>> dir(lambda:0) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] The function specific attributes are documented in https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy The same was not done for 'internal types': code objects and co_, frame objects and f_, traceback objects and tb_. Their namespaces are still frozen. -- Terry Jan Reedy