Jan-10-2019, 10:10 PM
(Jan-10-2019, 02:48 AM)stullis Wrote: Accessing attributes directly is taboo in most programming languages and is archetypal of OOP languages in particular.Perhaps I misunderstand you here, but I've been programming for quite some time in other languages (C++, PHP, Javascript) and have often seen client code directly change the public properties/attributes of classes. I'd hardly say it's taboo. I might be missing something?
(Jan-10-2019, 02:48 AM)stullis Wrote: Directly changing attributes without using the appropriate methods can generate bugs since any given method could update multiple attributes for consistency. The book likely mentions it because Python doesn't enforce privacy; so, respecting privacy is entirely on the programmer.In my experience, if you don't want someone changing the properties/attributes of an object, you make them private. Conversely, it seem super tedious and unnecessary to create a get & set method when you don't apply any restrictions. This is really the essence of my question. Why create all those trivial get/set functions if you don't have to?
(Jan-10-2019, 02:48 AM)stullis Wrote: That said, Pythonistas don't hammer on this too much. Generally, we don't use setters/getters in Python because they aren't needed, whereas other languages require them.This is really the answer I was kinda hoping for. Still, I'd like to honor the predominant python culture in any code I write.
(Jan-10-2019, 02:48 AM)stullis Wrote: Personally, if an attribute is private, it should either have a setter or only be set at instantiation. In the event it lacks a setter, reading it is okay but don't change it.Hear hear!