2013年2月23日 星期六

Python的private method與protected method



# protected method
   在method名稱前面加上"_"   // protected在方法名稱前面加上"底線(_)"

# private method
   在method名稱之前加上"__" // private在方法名稱前面加上"雙底線(__)"



My understanding of Python convention is

 _member  is protected
 __member  is private


@@ Options for if you control the parent class @@

- Make it protected instead of private since that seems like what you really
want
- Use a  getter(@property def _protected_access_to_member...)  to limit the
protected access


@@ If you don't control it @@

 Undo the name mangling.

If you dir(object) you will see names something like
_Class__member which is what Python does to leading __ to "make it private".
There isn't truly private in python. This is probably considered evil.


http://stackoverflow.com/questions/797771/python-protected-attributes



--------------------------------------------------------------------------
@@ Private Method @@

http://blog.roodo.com/thinkingmore/archives/13763931.html


http://stackoverflow.com/questions/3385317/private-variables-and-methods-in-python
http://blog.roodo.com/thinkingmore/archives/13763931.html

沒有留言:

張貼留言