Python Forum
when to make attributes private?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
when to make attributes private?
#7
(Jan-11-2019, 10:37 AM)Gribouillis Wrote: I almost never use the double underscore privacy mechanism such as __eggs
The wording privacy mechanism is maybe not best description for the double underscore,as it a has "little" or nothing to do with privacy.
It's really to avoid naming conflicts in subclasses,also called name mangling.

To take quick tour of _ and __.
private and public variables like Java does,dos not translate directly over to Python.

Single _
class Bar:
    def __init__(self):
        self.external = 99
        self._internal = 50
Use:
>>> obj = Bar()
>>> 
>>> # get
>>> obj._internal
50
>>> # set
>>> obj._internal = 444
>>> obj._internal
444
Have full access to the attribute value of that variable even it with _.
_ just as Waring hint merely an agreed upon convention.
Quote:Hey, this isn’t really meant to be a part of the public interface of this class.
Best to leave it alone.
Quote:Nothing is really private in python.
After all, we're all consenting adults here.

Double __
class Foo:
    def __init__(self):
        self.var_1 = 'hello'
        self.__var_2 = 'egg' # Not private,it's for name mangling when subclass

class Bar(Foo):
    def __init__(self):
        super().__init__()
        self.var_1 = 'world'
        self.__var_2 = 'spam'
Use:
>>> obj = Bar()
>>> 
>>> obj.var_1
'world'
>>> # var_1 "hello" is overridden and can never be restored
>>> 
>>> obj._Foo__var_2
'egg'
>>> obj._Bar__var_2
'spam'
>>> # var_2 can not be overridden in subclass,because of name mangling so both "egg" and "spam" can be reached
So i guess we could say that we private copy of original egg,but it still is possible to access or modify a variable that we call private copy Hand
Reply


Messages In This Thread
when to make attributes private? - by sneakyimp - Jan-10-2019, 01:08 AM
RE: when to make attributes private? - by stullis - Jan-10-2019, 02:48 AM
RE: when to make attributes private? - by sneakyimp - Jan-10-2019, 10:10 PM
RE: when to make attributes private? - by snippsat - Jan-11-2019, 04:04 AM
RE: when to make attributes private? - by snippsat - Jan-11-2019, 06:11 PM
RE: when to make attributes private? - by snippsat - Jan-11-2019, 07:13 PM
RE: when to make attributes private? - by nilamo - Jan-11-2019, 07:13 PM
RE: when to make attributes private? - by sneakyimp - Jan-21-2019, 02:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Lint and private var names PatM 1 726 Dec-15-2022, 05:08 PM
Last Post: deanhystad
  Unable to import Private Repo using setup.py Bob786 1 1,788 Sep-02-2021, 04:19 PM
Last Post: snippsat
  python 3 dns lookup private domain didact 1 2,612 Sep-19-2020, 06:01 PM
Last Post: bowlofred
  [split] Помощь по приватным ключам/Private key help sairam17519 0 1,627 Sep-07-2020, 12:55 PM
Last Post: sairam17519
  Download file from Private GitHub rep vinuvt 0 2,002 Jul-27-2020, 11:38 AM
Last Post: vinuvt
  Private package distribution abomination disadvantages research andreir 2 2,201 May-07-2020, 12:32 AM
Last Post: andreir
  Помощь по приватным ключам/Private key help vlrubl777 5 6,068 Mar-15-2019, 08:16 PM
Last Post: vlrubl777
  Fetching private ip address from instances of an autoscaling group deepsonune 0 3,302 May-18-2018, 10:32 AM
Last Post: deepsonune
  Inheritance private attributes vaison 5 12,888 May-03-2018, 09:22 AM
Last Post: vaison
  Can access class private variable? Michael 2 7,211 Aug-11-2017, 01:59 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020