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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:49:18+00:00 2026-05-10T15:49:18+00:00

Python gives us the ability to create ‘private’ methods and variables within a class

  • 0

Python gives us the ability to create ‘private’ methods and variables within a class by prepending double underscores to the name, like this: __myPrivateMethod(). How, then, can one explain this

>>>> class MyClass: ...     def myPublicMethod(self): ...             print 'public method' ...     def __myPrivateMethod(self): ...             print 'this is private!!' ... >>> obj = MyClass()  >>> obj.myPublicMethod() public method  >>> obj.__myPrivateMethod() Traceback (most recent call last):   File "<stdin>", line 1, in <module> AttributeError: MyClass instance has no attribute '__myPrivateMethod'  >>> dir(obj) ['_MyClass__myPrivateMethod', '__doc__', '__module__', 'myPublicMethod']  >>> obj._MyClass__myPrivateMethod() this is private!! 

What’s the deal?!

I’ll explain this a little for those who didn’t quite get that.

>>> class MyClass: ...     def myPublicMethod(self): ...             print 'public method' ...     def __myPrivateMethod(self): ...             print 'this is private!!' ... >>> obj = MyClass() 

I create a class with a public method and a private method and instantiate it.

Next, I call its public method.

>>> obj.myPublicMethod() public method 

Next, I try and call its private method.

>>> obj.__myPrivateMethod() Traceback (most recent call last):   File "<stdin>", line 1, in <module> AttributeError: MyClass instance has no attribute '__myPrivateMethod' 

Everything looks good here; we’re unable to call it. It is, in fact, ‘private’. Well, actually it isn’t. Running dir() on the object reveals a new magical method that Python creates magically for all of your ‘private’ methods.

>>> dir(obj) ['_MyClass__myPrivateMethod', '__doc__', '__module__', 'myPublicMethod'] 

This new method’s name is always an underscore, followed by the class name, followed by the method name.

>>> obj._MyClass__myPrivateMethod() this is private!! 

So much for encapsulation, eh?

In any case, I’d always heard Python doesn’t support encapsulation, so why even try? What gives?

  • 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. 2026-05-10T15:49:19+00:00Added an answer on May 10, 2026 at 3:49 pm

    The name scrambling is used to ensure that subclasses don’t accidentally override the private methods and attributes of their superclasses. It’s not designed to prevent deliberate access from outside.

    For example:

    >>> class Foo(object): ...     def __init__(self): ...         self.__baz = 42 ...     def foo(self): ...         print self.__baz ...      >>> class Bar(Foo): ...     def __init__(self): ...         super(Bar, self).__init__() ...         self.__baz = 21 ...     def bar(self): ...         print self.__baz ... >>> x = Bar() >>> x.foo() 42 >>> x.bar() 21 >>> print x.__dict__ {'_Bar__baz': 21, '_Foo__baz': 42} 

    Of course, it breaks down if two different classes have the same name.

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

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Does your hack look something like this? $('.fadebutton').click( function() {… May 12, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer A constant expression is not the same as a const-qualified… May 12, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer I have done something similar to method 2 in one… May 12, 2026 at 9:24 pm

Related Questions

Python gives us the ability to create 'private' methods and variables within a class
This may be silly, but it's been nagging the back of my brain for
Assume a game in which one rolls 20, 8-sided die, for a total number
I have been trying to make a case for using Python at my work.
$ mkdir foo $ cd foo $ hg init . $ hg log abort:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.