Re: Double sided double underscored variable names

Discussion in 'Python' started by Chris Angelico, Sep 11, 2012.

  1. On Wed, Sep 12, 2012 at 8:09 AM, Joshua Landau
    <> wrote:
    > If I were to use internal double-underscored names of the form
    > __BS_internalname__, would the compiled code be able to assume that no-one
    > had overwritten these variables and never will, even through modification
    > of, say, locals(). I ask because Python's docs seem to specify that double
    > sided double underscored names are strictly for Python and its special
    > names.


    Interesting. If you're compiling your code to Python, you may be able
    to, if necessary, adorn a user's variable name(s). I'd be inclined to
    use a single underscore notation like _BS_internalname_ and then, in
    the event of a collision (which would be incredibly unlikely unless
    someone's fiddling), rename the offending variable to
    _BS_BS_something_ - which of course you'd never yourself use. Would
    that serve?

    ChrisA
    Chris Angelico, Sep 11, 2012
    #1
    1. Advertising

  2. Sorry for breaking threading, but Joshua's post does not show up on my
    usenet provider.

    On Wed, 12 Sep 2012 08:22:17 +1000, Chris Angelico wrote:

    > On Wed, Sep 12, 2012 at 8:09 AM, Joshua Landau
    > <> wrote:
    >>
    >> If I were to use internal double-underscored names of the form
    >> __BS_internalname__, would the compiled code be able to assume that
    >> no-one had overwritten these variables and never will,


    Certainly not. It is virtually never possible to make that assumption in
    Python. Nearly everything can be shadowed at runtime.

    (One exception is local variables of a function, which can only be seen
    from inside that function. But you don't need to wrap local variable
    names in underscores to treat them as local.)

    Dunder ("Double leading and trailing UNDERscore") names are normal names
    subject to the same rules as anything else in Python (with one
    exception), which means you can modify them in all sorts of ways at
    runtime:

    py> _int = int
    py> class MyInt(_int):
    .... def __add__(self, other):
    .... return 42
    ....
    py> int = MyInt
    py> a = int("10000")
    py> a + 1
    42
    py> type(a).__add__ = lambda self, other: 23
    py> a + 1
    23


    The one exception how dunder names are treated specially is that Python
    uses a short-cut for name-lookup which means you cannot override them on
    a per-instance basis like normal other attribute names.



    >> even through modification of, say, locals().


    Modifying locals() is an implementation detail which may not be supported.



    >> I ask because Python's docs seem to
    >> specify that double sided double underscored names are strictly for
    >> Python and its special names.


    Dunder names are reserved for use by Python's built-ins and syntax, that
    is all.


    > Interesting. If you're compiling your code to Python, you may be able
    > to, if necessary, adorn a user's variable name(s). I'd be inclined to
    > use a single underscore notation like _BS_internalname_ and then, in the
    > event of a collision (which would be incredibly unlikely unless
    > someone's fiddling), rename the offending variable to _BS_BS_something_
    > - which of course you'd never yourself use. Would that serve?


    This seems to be a mere extension of the name-mangling that occurs with
    leading-only double-underscore attributes like self.__spam. This can be a
    right PITA at times, and offers so little protection that it isn't
    worthwhile. __names only protect against *accidental* name collisions,
    and not all of those.


    --
    Steven
    Steven D'Aprano, Sep 12, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. agb

    client-sided control

    agb, Aug 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    Natty Gur
    Aug 27, 2003
  2. freesoft_2000

    Double-Sided

    freesoft_2000, Feb 1, 2005, in forum: Java
    Replies:
    1
    Views:
    452
    Mario Winterer
    Feb 1, 2005
  3. TJ
    Replies:
    1
    Views:
    364
  4. Shadow Lynx
    Replies:
    10
    Views:
    5,400
    Dave Anderson
    Aug 9, 2006
  5. Chris Angelico
    Replies:
    3
    Views:
    207
    Joshua Landau
    Sep 12, 2012
Loading...

Share This Page