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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:14:29+00:00 2026-05-25T16:14:29+00:00

In other languages, a general guideline that helps produce better code is always make

  • 0

In other languages, a general guideline that helps produce better code is always make everything as hidden as possible. If in doubt about whether a variable should be private or protected, it’s better to go with private.

Does the same hold true for Python? Should I use two leading underscores on everything at first, and only make them less hidden (only one underscore) as I need them?

If the convention is to use only one underscore, I’d also like to know the rationale.

Here’s a comment I left on JBernardo’s answer. It explains why I asked this question and also why I’d like to know why Python is different from the other languages:

I come from languages that train you to think everything should be only as public as needed and no more. The reasoning is that this will reduce dependencies and make the code safer to alter. The Python way of doing things in reverse — starting from public and going towards hidden — is odd to me.

  • 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-05-25T16:14:29+00:00Added an answer on May 25, 2026 at 4:14 pm

    When in doubt, leave it "public" – I mean, do not add anything to obscure the name of your attribute. If you have a class with some internal value, do not bother about it. Instead of writing:

    class Stack(object):
    
        def __init__(self):
            self.__storage = [] # Too uptight
    
        def push(self, value):
            self.__storage.append(value)
    

    write this by default:

    class Stack(object):
    
        def __init__(self):
            self.storage = [] # No mangling
    
        def push(self, value):
            self.storage.append(value)
    

    This is for sure a controversial way of doing things. Python newbies hate it, and even some old Python guys despise this default – but it is the default anyway, so I recommend you to follow it, even if you feel uncomfortable.

    If you really want to send the message "Can’t touch this!" to your users, the usual way is to precede the variable with one underscore. This is just a convention, but people understand it and take double care when dealing with such stuff:

    class Stack(object):
    
        def __init__(self):
            self._storage = [] # This is ok, but Pythonistas use it to be relaxed about it
    
        def push(self, value):
            self._storage.append(value)
    

    This can be useful, too, for avoiding conflict between property names and attribute names:

     class Person(object):
         def __init__(self, name, age):
             self.name = name
             self._age = age if age >= 0 else 0
         
         @property
         def age(self):
             return self._age
         
         @age.setter
         def age(self, age):
             if age >= 0:
                 self._age = age
             else:
                 self._age  = 0
    

    What about the double underscore? Well, we use the double underscore magic mainly to avoid accidental overloading of methods and name conflicts with superclasses’ attributes. It can be pretty valuable if you write a class to be extended many times.

    If you want to use it for other purposes, you can, but it is neither usual nor recommended.

    EDIT: Why is this so? Well, the usual Python style does not emphasize making things private – on the contrary! There are many reasons for that – most of them controversial… Let us see some of them.

    Python has properties

    Today, most OO languages use the opposite approach: what should not be used should not be visible, so attributes should be private. Theoretically, this would yield more manageable, less coupled classes because no one would change the objects’ values recklessly.

    However, it is not so simple. For example, Java classes have many getters that only get the values and setters that only set the values. You need, let us say, seven lines of code to declare a single attribute – which a Python programmer would say is needlessly complex. Also, you write a lot of code to get one public field since you can change its value using the getters and setters in practice.

    So why follow this private-by-default policy? Just make your attributes public by default. Of course, this is problematic in Java because if you decide to add some validation to your attribute, it would require you to change all:

    person.age = age;
    

    in your code to, let us say,

    person.setAge(age);
    

    setAge() being:

    public void setAge(int age) {
        if (age >= 0) {
            this.age = age;
        } else {
            this.age = 0;
        }
    }
    

    So in Java (and other languages), the default is to use getters and setters anyway because they can be annoying to write but can spare you much time if you find yourself in the situation I’ve described.

    However, you do not need to do it in Python since Python has properties. If you have this class:

     class Person(object):
         def __init__(self, name, age):
             self.name = name
             self.age = age
    

    …and then you decide to validate ages, you do not need to change the person.age = age pieces of your code. Just add a property (as shown below)

     class Person(object):
         def __init__(self, name, age):
             self.name = name
             self._age = age if age >= 0 else 0
         
         @property
         def age(self):
             return self._age
         
         @age.setter
         def age(self, age):
             if age >= 0:
                 self._age = age
             else:
                 self._age  = 0
    

    Suppose you can do it and still use person.age = age, why would you add private fields and getters and setters?

    (Also, see Python is not Java and this article about the harms of using getters and setters.).

    Everything is visible anyway – and trying to hide complicates your work

    Even in languages with private attributes, you can access them through some reflection/introspection library. And people do it a lot, in frameworks and for solving urgent needs. The problem is that introspection libraries are just a complicated way of doing what you could do with public attributes.

    Since Python is a very dynamic language, adding this burden to your classes is counterproductive.

    The problem is not being possible to see – it is being required to see

    For a Pythonista, encapsulation is not the inability to see the internals of classes but the possibility of avoiding looking at it. Encapsulation is the property of a component that the user can use without concerning about the internal details. If you can use a component without bothering yourself about its implementation, then it is encapsulated (in the opinion of a Python programmer).

    Now, if you wrote a class you can use it without thinking about implementation details, there is no problem if you want to look inside the class for some reason. The point is: your API should be good, and the rest is details.

    Guido said so

    Well, this is not controversial: he said so, actually. (Look for "open kimono.")

    This is culture

    Yes, there are some reasons, but no critical reason. This is primarily a cultural aspect of programming in Python. Frankly, it could be the other way, too – but it is not. Also, you could just as easily ask the other way around: why do some languages use private attributes by default? For the same main reason as for the Python practice: because it is the culture of these languages, and each choice has advantages and disadvantages.

    Since there already is this culture, you are well-advised to follow it. Otherwise, you will get annoyed by Python programmers telling you to remove the __ from your code when you ask a question in Stack Overflow 🙂

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

Sidebar

Related Questions

I know that some other languages, such as PHP , support a concept of
I know there are libs in other languages that can take a string that
In other languages you can use strings as keys - PHP: $array['string'] = 50;
In Flex (and many other languages) a function/method of a class can be declared
In C# and in Java (and possibly other languages as well), variables declared in
Delphi (and probably a lot of other languages) has class helpers. These provide a
I know with python and a couple other languages there is a way to
Can C++ slicing apply to other languages too, like Java/C#?
I know Python (and a bunch of other languages) and I think it might
In a C# (feel free to answer for other languages) loop, what's the difference

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.