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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:37:54+00:00 2026-05-18T22:37:54+00:00

In Python, I have the following example class: class Foo: self._attr = 0 @property

  • 0

In Python, I have the following example class:

class Foo:
    self._attr = 0

    @property
    def attr(self):
        return self._attr

    @attr.setter
    def attr(self, value):
        self._attr = value

    @attr.deleter
    def attr(self):
        del self._attr

As you can see, I have a simple "private" attribute "_attr" and a property to access it. There is a lot of codes to declare a simple private attribute and I think that it’s not respecting the "KISS" philosophy to declare all attributes like that.

So, why not declare all my attributes as public attributes if I don’t need a particular getter/setter/deleter?

My answer will be:
Because the principle of encapsulation (OOP) says otherwise!

What is the best way?

  • 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-18T22:37:54+00:00Added an answer on May 18, 2026 at 10:37 pm

    Typically, Python code strives to adhere to the Uniform Access Principle. Specifically, the accepted approach is:

    • Expose your instance variables directly, allowing, for instance, foo.x = 0, not foo.set_x(0)
    • If you need to wrap the accesses inside methods, for whatever reason, use @property, which preserves the access semantics. That is, foo.x = 0 now invokes foo.set_x(0).

    The main advantage to this approach is that the caller gets to do this:

    foo.x += 1
    

    even though the code might really be doing:

    foo.set_x(foo.get_x() + 1)
    

    The first statement is infinitely more readable. Yet, with properties, you can add (at the beginning, or later on) the access control you get with the second approach.

    Note, too, that instance variables starting with a single underscore are conventionally private. That is, the underscore signals to other developers that you consider the value to be private, and they shouldn’t mess with it directly; however, nothing in the language prevents them from messing with it directly.

    If you use a double leading underscore (e.g., __x), Python does a little obfuscation of the name. The variable is still accessible from outside the class, via its obfuscated name, however. It’s not truly private. It’s just kind of … more opaque. And there are valid arguments against using the double underscore; for one thing, it can make debugging more difficult.

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

Sidebar

Related Questions

Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return
I am a newbie to Python and have come across the following example in
In Python compiled regex patterns have a findall method that does the following: Return
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have a question about using os.execvp in Python. I have the following bit
When using IF statements in Python, you have to do the following to make
Does python have the ability to create dynamic keywords? For example: qset.filter(min_price__usd__range=(min_price, max_price)) I
If I have Python code class A(): pass class B(): pass class C(A, B):
I have the following simple methods for writing a python object to a file
In LISP-like languages all language constructs are first-class citizens. Consider the following example in

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.