What python idioms for private, protected and public?

Michael Schneider michaelschneider at fuse.net
Thu Sep 29 16:08:28 CEST 2005


I have been following this thread with great interest.

I have been coding in C++ since the late 80's and Java since the late 90's.


I do use private in these languages, with accessors to get at internal 
data.

This has become an ingrained idiom for me.  When I create a python 
object, it is natural for me to want  to use familiar idioms.


Hare are the places where private is useful to me:


Design Intent:

1) mark an object as dirty in a setter (anytime the object is changed, 
the dirty flag is set without requiring a user to set the dirty flag

2) enforce value constraints (even if just during debugging)

3) lazy init, don't bring the data in until needed

4) adding debug info

5) .... more here????


I do write code that violates private
	- memory ptr access in C++
	- reflection in java
	- aspects in java

I usually violate private when adding an aspect to a class, and
I don't want this code in every class.  Example,  persistence.


I really like the C# properties, you can access data with a data
accessor, but add functionality is triggered when accessing the data.

I like the data access syntax, better then the set/get functions.  I 
need the functionality to achieve the design goals above, so i use 
function, but the are ugly, especially in math code.


It would be easy for me to say "Add public and private to python so I
can code the way that I am used to".  What are some python alternatives 
to achieve the design intents specified above above?

Thanks
Mike



More information about the Python-list mailing list