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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:04:30+00:00 2026-05-18T00:04:30+00:00

I have a question about righteous way of programming in Python… Maybe there can

  • 0

I have a question about righteous way of programming in Python… Maybe there can be several different opinions, but here it goes:

Let’s say I have a class with a couple of private attributes and that I have implemented two getters/setters (not overloading __getattr__ and __setattr__, but in a more “Java-tistic” style):

class MyClass:

    def __init__(self):   
        self.__private1 = "Whatever1"

    def setPrivate1(self, private1):
        if isinstance(private1, str) and (private1.startswith("private")):
            self.__private1 = private1
        else:
            raise AttributeError("Kaputt")

    def getPrivate1(self):
        return self.__private1

Now let’s say a few lines below, in another method of the same class, I need to re-set the value of that “__private1”. Since it’s the same class, I still have direct access to the private attribute self.__private1.

My question is: Should I use:

self.setPrivate1("privateBlaBlaBla")

or should I access directly as:

self.__private1 ="privateBlaBlaBla"

since I am the one setting the new value, I know that said value (“privateBlaBlaBla”) is correct (an str() that starts with “private”), so it is not going to leave the system inconsistent. On the other hand, if another programmer takes my code, and needs to change the functionality for the self.__private1 attribute, he will need to go through all the code, and see if the value of __private1 has been manually set somewhere else.

My guess is that the right thing to do is to always using the setPrivate1 method, and only access directly the __private1 variable in the get/set, but I’d like to know the opinion of more experienced Python programmers.

  • 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-18T00:04:31+00:00Added an answer on May 18, 2026 at 12:04 am

    You can’t present a classic example of bad Python and then expect people to have opinions on what do to about it. Use getters and setters.

    class MyClass:
        def __init__(self):   
            self._private1 = "Whatever1"
    
        @property
        def private1(self):
            return self._private1
    
        @private1.setter
        def private1(self, value):
            self._private1 = value
    

    A side comment — using double underscore names can be confusing, because Python actually mangles the name to stop you accessing them from outside the class. This provides no real security, but causes no end of headaches. The easiest way to avoid the headaches is to use single-underscore names, which is basically a universal convention for private. (Ish.)


    If you want an opinion — use properties =). If you want an opinion on your JavaPython monstrosity, I would use the setter — after all, you’ve written it, that’s what it’s there for! There’s no obvious benefit to setting the variable by hand, but there are several drawbacks.

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

Sidebar

Related Questions

No related questions found

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.