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 8537943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:01:21+00:00 2026-06-11T11:01:21+00:00

I write a test code of python like: class Parent(object): @classmethod def dispatch(klass): print

  • 0

I write a test code of python like:

class Parent(object):
    @classmethod
    def dispatch(klass):
        print 'klass = %s' % klass
        return klass().__dispatch()
    def __dispatch(self):
        print 'This Parent.__dispatch()'
        print self


class Child(Parent):
    def __dispatch(self):
        print 'This Child.__dispatch()'
        print self


if __name__=='__main__':
    print 'Calling Parent.dispatch() ...\n'
    Parent.dispatch()
    print ''
    print 'Calling Child.dispatch() ...\n'
    Child.dispatch()
    print '\n-END'

And the output is:

 Calling Parent.dispatch() ...

 klass = <class '__main__.Parent'> 
 This Parent.__dispatch()
 <__main__.Parent object at 0x0000000002D3A2E8>

 Calling Child.dispatch() ...

 klass = <class '__main__.Child'> 
 This Parent.__dispatch()
 <__main__.Child object at 0x0000000002D3A2E8>

 -END

It’s very strange why the overwrote method ‘__dispatch(self)’ of Child class was not called.

Does anyone can explain about this?

Thank you.

  • 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-11T11:01:23+00:00Added an answer on June 11, 2026 at 11:01 am

    Methodnames that start with a double underscore are automatically mangled; internally, the string _ClassName is prepended to the method name:

    >>> class Foo(object):
    ...     def __bar(self):
    ...         pass
    ... 
    >>> Foo.__dict__.keys()
    ['__dict__', '__module__', '__weakref__', '__doc__', '_Foo__bar']
    

    This renaming also is done for anything referencing this method name in any of the other methods in the class.

    Thus, your __dispatch() method is renamed to _Parent__dispatch(), and the dispatch() method is altered to call self._Parent__dispatch() instead. Similarly, your Child class has a _Child__dispatch() method, and it thus does not override the _Parent__dispatch() method of it’s super-class.

    This is why you see the results you see; rename your __dispatch() methods to _dispatch() (only one underscore) and it’ll work as expected.

    Why does python do this? It’s a form of providing private attributes and methods, that cannot be accidentally overridden by classes that inherit from them. See private name mangling in the Python expression reference.

    The PEP 8 Python Style Guide has this to say about private name mangling:

    If your class is intended to be subclassed, and you have attributes
    that you do not want subclasses to use, consider naming them with
    double leading underscores and no trailing underscores. This invokes
    Python’s name mangling algorithm, where the name of the class is
    mangled into the attribute name. This helps avoid attribute name
    collisions should subclasses inadvertently contain attributes with the
    same name.

    Note 1: Note that only the simple class name is used in the mangled
    name, so if a subclass chooses both the same class name and attribute
    name, you can still get name collisions.

    Note 2: Name mangling can make certain uses, such as debugging and
    __getattr__(), less convenient. However the name mangling algorithm
    is well documented and easy to perform manually.

    Note 3: Not everyone likes name mangling. Try to balance the need to
    avoid accidental name clashes with potential use by advanced callers.

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

Sidebar

Related Questions

I'm trying to write something like: class MyClass(object): @staticmethod def test(x, y, z=None): if
the code here below: #!/usr/bin/env python import wx class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None,
I'm a student, and I'm trying to write and run some test code for
I'm trying to write a program to test student code against a good implementation.
I'm trying to write a unit test for a piece of code that generates
I write this code to get Dunnet anova post hoc test import rpy2.robjects as
i just join phonegap, i want a built test application. i write some code
document.write('test'); Will work, and print 'test' to the document. document.write(d.toDateString()); Does nothing. d is
I would like to rewrite the python code example from the book to C#
I'd like to write some Python unit tests for my Google App Engine. How

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.