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


Groups > comp.lang.python > #76203

Re: Arbitrary dunder attributes (was Re: 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.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@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; 'argument': 0.05; 'attribute': 0.07; 'class,': 0.07; '*is*': 0.09; 'attributes': 0.09; 'defines': 0.09; 'feature,': 0.09; 'function,': 0.09; 'latter': 0.09; 'namespace': 0.09; 'operator,': 0.09; 'subject:instance': 0.09; 'used.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; "wouldn't": 0.14; '*you*': 0.16; '__lt__': 0.16; 'attributes,': 0.16; 'crude': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func': 0.16; 'ought': 0.16; 'subject: \n ': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'underscore.': 0.16; 'applies': 0.16; ':-)': 0.16; 'language': 0.16; 'wrote:': 0.18; 'wed,': 0.18; "python's": 0.19; '(the': 0.22; 'aug': 0.22; 'issue.': 0.22; 'cc:addr:python.org': 0.22; 'documented': 0.24; 'instead.': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'define': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'chris': 0.29; 'generally': 0.29; 'possibility': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '13,': 0.31; "d'aprano": 0.31; 'names.': 0.31; 'ones.': 0.31; 'public.': 0.31; 'steven': 0.31; 'with,': 0.31; 'class': 0.32; 'stuff': 0.32; 'regular': 0.32; '(most': 0.33; 'becomes': 0.33; 'subject:from': 0.34; 'could': 0.34; 'subject: (': 0.35; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'like,': 0.36; 'picking': 0.36; 'doing': 0.36; "didn't": 0.36; 'method': 0.36; 'possible': 0.36; 'should': 0.36; 'members.': 0.37; 'easily': 0.37; 'being': 0.38; 'sometimes': 0.38; 'system,': 0.38; 'whatever': 0.38; 'pm,': 0.38; 'anything': 0.39; 'called': 0.40; 'even': 0.60; 'easy': 0.60; 'most': 0.60; 'free': 0.61; 'privacy': 0.61; 'simply': 0.61; 'first': 0.61; 'name': 0.63; 'stand': 0.64; 'different': 0.65; 'useful.': 0.68; '10:11': 0.84; 'batchelder': 0.84; 'collision.': 0.84; 'significance': 0.84; 'tie': 0.84; 'on?': 0.91; 'thing,': 0.91; 'this;': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=sfxDUYMHUJZIrbW1jZ6sdteuxYkl7RHLerMZV2HA1Eg=; b=V1XEQ8mcNasKILXnZG76jAVjvdGGbGfxy1MBOnh8rP6L4jdIUPlGykHjU6nE9v5v7C 5q3nF1s1A+984F8xqCRICfbZ50TQ/ZUink9hnFwjwsMv1P5SpjMO7Ed7aJC4wq5m8WMT 2btE485kaulZ4OTAvT9i/U9KGJ/MMJcEnaDTY3akVq7wYrqo9HG3vw2oxM8j11xxEYSI J5vTv/1fQpjLviarN0BBRAn9JIwZDIciPfEKPDlLKNx47jdVZ0iq6bp6bZ66kPMS2d7M e5QX7W0ad0UWZfyDQZrrAgaX9DUYFlyDqPAzqXRHaNiZCJtXfgtPJwSRbruuWhw1nEXi fmGQ==
MIME-Version 1.0
X-Received by 10.50.30.72 with SMTP id q8mr49039157igh.14.1407938960053; Wed, 13 Aug 2014 07:09:20 -0700 (PDT)
In-Reply-To <53eb6431$0$29982$c3e8da3$5496439d@news.astraweb.com>
References <mailman.12915.1407923500.18130.python-list@python.org> <53eb6431$0$29982$c3e8da3$5496439d@news.astraweb.com>
Date Thu, 14 Aug 2014 00:09:20 +1000
Subject Re: Arbitrary dunder attributes (was Re: odd difference calling function from class or instance variable)
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.12925.1407938963.18130.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1407938963 news.xs4all.nl 2886 [2001:888:2000:d::a6]:49386
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76203

Show key headers only | View raw


On Wed, Aug 13, 2014 at 11:12 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Chris Angelico wrote:
>
>> 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;
>
> Why do you say they are intended to be private? Is that documented
> somewhere, or do you mean that if you designed the system, *you* would have
> intended them to be private? :-)

Good point - the latter :) However, that applies only to the one
leading underscore. Whether they're _self/_func or self/func, they
could be regular attributes without being dunder ones.

>> is it really something
>> that has language-level significance on par with __lt__ and so on?
>
> To be honest, I didn't even know about __self__, but I understood __func__
> to be public. And yes, it should be public: being able to introspect a
> method and find the function it came from is a good thing, and sometimes
> useful.

I stand corrected on the privacy issue. But introspection would be
just as easy if it were called func instead. Picking up Ned's wording:

On Wed, Aug 13, 2014 at 10:11 PM, Ned Batchelder <ned@nedbatchelder.com> wrote:
> This is a crude namespacing: Python can use any name it likes so long as its
> a dunder name, and I can use any name I like, so long as it isn't.

This concept generally applies to Python doing stuff with my class. If
I define a class that I want a built-in function, operator, or
language feature, to work with, I define a dunder attribute (most
often a method) to tie in with that - my class, Python's
functionality.

When Python defines the class, it's free to do whatever it likes with
naming. There are public methods and attributes, named nice and simply
because there's no possibility of collision. The bound method object
is like this; it could easily have any number of simply-named
attributes, and most of us wouldn't even be aware of them, much less
concerned that we now can't use those names. We can't possibly
collide, because this is a completely different class from anything
I'm working with.

Even namedtuple, where it *is* possible to have collisions, doesn't go
dunder - it has single-underscore public members. That's a deliberate
decision to use a namespace other than the normal one of simply-named
members; and dunders weren't used. In the case of __self__ and
__func__, what's the advantage of this tagging?

ChrisA

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


Thread

Arbitrary dunder attributes (was Re: odd difference calling function from class or instance variable) Chris Angelico <rosuav@gmail.com> - 2014-08-13 19:51 +1000
  Re: Arbitrary dunder attributes (was Re: odd difference calling function from class or instance variable) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-13 23:12 +1000
    Re: Arbitrary dunder attributes (was Re: odd difference calling function from class or instance variable) Chris Angelico <rosuav@gmail.com> - 2014-08-14 00:09 +1000

csiph-web