Correct Way to Write in Python

Chris Angelico rosuav at gmail.com
Sat Aug 3 12:21:07 EDT 2013


On Sat, Aug 3, 2013 at 4:59 PM, Sagar Varule <punk.sagar at gmail.com> wrote:
> Your explanation for private and public access modifier was awesome as I was having harding time finding why we dont have access modifier for python....Thanks a lot

It's a huge saving in time and effort. The C++ convention is: Make
everything private, then hand-write getters and setters for them all,
just in case you want to put extra code onto them. (I don't know C#
but it seems to be pretty much the same.) The Python convention is:
We're all consenting adults. Make stuff public, then if you need to
add code to something, make a @property that simulates the old
behaviour.

Personally, I've started to adopt the Python style in my C++ code as
well. I use struct instead of class, avoid making anything private,
and document the things that must not be done - for instance, I
created a class that had one particular pointer that must never be
advanced other than by the provided member function, but may be
retarded. No reason to fiddle with member privacy even there.

(The same technique has benefit in a quite different area, too:
separation of code and data. Not in C++, but I have systems that let
me load new code into a running process, and there it's extremely
helpful to just do everything as a simple structure, and then the new
code can slide in and work with the old data structure, happily
extending it with whatever it now needs. Again, everything's public.)

ChrisA



More information about the Python-list mailing list