Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:26:49+00:00 2026-06-11T19:26:49+00:00

I know what double underscore means for Python class attributes/methods, but does it mean

  • 0

I know what double underscore means for Python class attributes/methods, but does it mean something for method argument?

It looks like you cannot pass argument starting with double underscore to methods. It is confusing because you can do that for normal functions.

Consider this script:

def egg(__a=None):
    return __a

print "egg(1) =",
print egg(1)
print


class Spam(object):

    def egg(self, __a=None):
        return __a

print "Spam().egg(__a=1) =",
print Spam().egg(__a=1)

Running this script yields:

egg(1) = 1

Spam().egg(__a=1) =
Traceback (most recent call last):
  File "/....py", line 15, in <module>
    print Spam().egg(__a=1)
TypeError: egg() got an unexpected keyword argument '__a'

I checked this with Python 2.7.2.


Some other examples

This works:

def egg(self, __a):
    return __a


class Spam(object):

    egg = egg

Spam().egg(__a=1)

This does not:

class Spam(object):

    def _egg(self, __a=None):
        return __a

    def egg(self, a):
        return self._egg(__a=a)

Spam().egg(1)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:26:51+00:00Added an answer on June 11, 2026 at 7:26 pm

    Name mangling applies to all identifiers with leading double underscores, regardless of where they occur (second to last sentence in that section):

    This transformation is independent of the syntactical context in which the identifier is used.

    This is simpler to implement and to define, and more consistent. It may seem stupid, but the whole name mangling deal is an ugly little hack IMHO; and you’re not expected to use names like that for anything except attributes/methods anyway.

    Spam().egg(_Spam__a=1), as well as Spam().egg(1), does work. But even though you can make it work, leading underscores (any number of them) have no place in parameter names. Or in any local variable (exception: _) for that matter.

    Edit: You appear to have found a corner case nobody ever considered. The documentation is imprecise here, or the implementation is flawed. It appears keyword argument names are not mangled. Look at the bytecode (Python 3.2):

    >>> dis.dis(Spam.egg)
      3           0 LOAD_FAST                0 (self)
                  3 LOAD_ATTR                0 (_egg)
                  6 LOAD_CONST               1 ('__a') # keyword argument not mangled
                  9 LOAD_FAST                1 (a)
                 12 CALL_FUNCTION          256
                 15 RETURN_VALUE
    >>> dis.dis(Spam._egg)
      2           0 LOAD_FAST                1 (_Spam__a) # parameter mangled
                  3 RETURN_VALUE
    

    This may be rooted in the fact that keyword arguments are equivalent to passing a dict (in this case {'__a': 1}) whose keys wouldn’t be mangled either. But honestly, I’d just call it an ugly corner case in an already ugly special case and move on. It’s not important because you shouldn’t use identifiers like that anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the attributes of class which are declared by double underscore __
So in the meanwhile we know that double-checked-locking as is does not work in
Does anyone know where I can find that algorithm? It takes a double and
What does it mean when a PHP function name begins with an underscore? for
I know about the double margin bug, but this is different.. the scenario is
Does anyone know of a double valued gauge in wxPython? I would like to
Does any body know why some columns in postgres is shown as double quotes
I know that Double is a a wrapper class, and it wraps double number.
I know about cmd-double-click , and use it all the time, it's great. But
Does anyone know whether a double is implicitly cast to double? ( Nullable type)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.