Re: To make a method or attribute private
On 17/01/13 11:34, iMath wrote:
> To make a method or attribute private (inaccessible from the outside),
> simply start its name with two underscores
> ----《Beginning Python From Novice to Professional》
> but there is another saying goes:
> Beginning a variable name with a single underscore indicates that the
> variable should be treated as ‘private’.
> I test both these 2 rules ,it seems only names that start with two
> underscores are REAL private methods or attributes .
Python does not have a REAL private methods/attributes. The double
leading underscore is meant to trigger name mangling to avoid naming
collisions with subclasses, the method/attribute is still accessible
using the mangled name:
You generally only use double leading underscores when your private
method/attribute is in a very high risk of having naming clashes with
> so what is your opinion about single leading underscore and private
> methods or attributes？
We're all consenting adults. Use methods/attributes with single or
double leading underscore at your own risk.
Most programming languages do not actually have a private attribute that
is totally inaccessible from outside, there are usually ways work around
access restrictions, usually using reflections or pointers. Python only
makes it easy to do so by making private variables only a convention.
|All times are GMT. The time now is 04:13 AM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.