Python Forum
when to make attributes private?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
when to make attributes private?
#1
I've been working through an introductory python book. I've got quite a bit of experience coding in other languages, but am new to Python. My book seems to have some odd notions about when to make attributes private and suggests all changes to an object's attributes should be done through methods. That sounds pretty tedious to me (creating all the accessors) but I was hoping to get some idea of what the python world things of its advice. The book says:
Quote:Understanding When to Implement Privacy
So now that you know how to use privacy, should you make every attribute in every class private to protect them from the outside world? Well, no. Privacy is like a fine spice: Used sparingly, it can greatly improve what you're making. Make private any method you don't want a client to invoke. If it's critical that an attribute never be accessed directly by a client, you can make it private. But keep this to a minimum, as creating private attributes is rare in Python. The philosophy among programmers is to trust that clients will sue an object's methods and not directly alter its attributes.

When you write a class:
* Create methods so that clients won't need to directly access an object's attributes
* Use privacy only for those attributes and methods that are completely internal to the operation of objects.
*
When you use an object:
* Minimize the direct reading of an object's attributes
* Avoid directly altering an object's attributes
* Never directly access an object's private attributes or methods

I added the italics. Now most of this makes sense but I've never been told not to directly access an object's attributes. Like I said, creating accessors for every attribute seems extremely tedious. I an appreciate concealing the sausage factory of a class with privacy, but this seems a bit strong.

Any comments or documentation welcome.
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 701 Dec-15-2022, 05:08 PM
Last Post: deanhystad
  Unable to import Private Repo using setup.py Bob786 1 1,767 Sep-02-2021, 04:19 PM
Last Post: snippsat
  python 3 dns lookup private domain didact 1 2,578 Sep-19-2020, 06:01 PM
Last Post: bowlofred
  [split] Помощь по приватным ключам/Private key help sairam17519 0 1,613 Sep-07-2020, 12:55 PM
Last Post: sairam17519
  Download file from Private GitHub rep vinuvt 0 1,981 Jul-27-2020, 11:38 AM
Last Post: vinuvt
  Private package distribution abomination disadvantages research andreir 2 2,166 May-07-2020, 12:32 AM
Last Post: andreir
  Помощь по приватным ключам/Private key help vlrubl777 5 5,973 Mar-15-2019, 08:16 PM
Last Post: vlrubl777
  Fetching private ip address from instances of an autoscaling group deepsonune 0 3,280 May-18-2018, 10:32 AM
Last Post: deepsonune
  Inheritance private attributes vaison 5 12,789 May-03-2018, 09:22 AM
Last Post: vaison
  Can access class private variable? Michael 2 7,197 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