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

  • SEARCH
  • Home
  • 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 9212749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:35:47+00:00 2026-06-18T01:35:47+00:00

I was trying out NVI(Non-Virtual Interface) Idiom in python, and noticed that private(double underscore)

  • 0

I was trying out NVI(Non-Virtual Interface) Idiom in python, and noticed that private(double underscore) methods don’t seem to be acting as virtual.

class A(object):
    def a(self):
        print "in A.a"
    self.b()
    self.__b()
    self._b()

def _b(self):
    print "in A._b"

def __b(self):
    print "in A.__b"

def b(self):
    print "in A.b"

class B(A):
def __b(self):
    print "in B.__b"

def b(self):
    print "in B.b"

def _b(self):
    print "in B._b"

>>> a=A()
>>> b=B()
>>> a.a()
in A.a
in A.b
in A.__b
in A._b
>>> b.a()
in A.a
in B.b
in A.__b
in B._b

I am guessing this may have been because of name mangling for double underscore methods, but it is counter-intuitive. Further, confusion arises from python documentation “(For C++ programmers: all methods in Python are effectively virtual.)”.

  • 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-18T01:35:48+00:00Added an answer on June 18, 2026 at 1:35 am

    As the documentation says, double-leading-underscore methods are for class-private members. They are private to the specific class in which they are used, not to its class inheritance subtree. That is the point: they are designed for class-specific variables whose values you don’t want subclass definitions to override, or subclass methods to access.

    You are correct that the name-mangling is the source of the behavior you describe. All Python methods are indeed virtual in that they can be overridden. The double-underscore methods just make access from outside the class harder by mangling the name. You could override A.__b by defining a method _A__b in class B. That would be a bad idea because it would be subverting the purpose of using double-underscores in the first place, but it is possible.

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

Sidebar

Related Questions

After trying out various combinations for the glBlendFunc, I've noticed that most of them
trying out upgrading antlr4, I have 2 lines in the grammar that produce the
I am trying out this simple code. What I want is that when a
Trying out Linq for NHibernate and first noticed this: var list1 = (from ut
When trying out http://www.feedly.com/ I noticed it automatically recognized my Twitter-name. How?
I am trying out Resharper and i notice that it is recommending to set
I am trying out DevExpress Xtrareports, but have asimple problem that I am not
Trying out some android programming that has gone fine up until this point. I'm
While trying out HippoMocks (Cygwin, GCC 4.5.3, CppUnit) to mock an interface, one of
Just trying out Mongoid at the moment, I've run into an issue that's probably

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.