开发者

What is the Pythonic way to use private variables?

I recently posted a question on stackoverflow and I got a resolution.

Some one suggested to me about the coding style and I haven't received further input. I have the following question with reference to the prior qu开发者_C百科ery.

  1. How can we declare private variables inside a class in python? I thought that by using a double underscore (__) the variable is treated as private. Please correct me.

  2. As per the suggestion received before, we don't have to use a getter or setter method. Shouldn't we use a getter or setter or both? Please let me know your suggestion on this one.


Everything is public in Python, the __ is a suggestion by convention that you shouldn't use that function as it is an implementation detail.

This is not enforced by the language or runtime in any way, these names are decorated in a semi-obfuscated way, but they are still public and still visible to all code that tries to use them.

Idiomatic Python doesn't use get/set accessors, it is duplication of effort since there is no private scope.

You only use accessors when you want indirect access to a member variable to have code around it, and then you mark the member variable with __ as the start of its name and provide a function with the actual name.

You could go to great lengths with writing reams of code to try and protect the user from themselves using Descriptors and meta programming, but in the end you will end up with more code that is more to test and more to maintain, and still no guarantee that bad things won't happen. Don't worry about it - Python has survived 20 years this way so far, so it can't be that big of a deal.


PEP 8 (http://www.python.org/dev/peps/pep-0008/) has a section "Designing for inheritance" that should address most of these concerns.

To quote:

"We don't use the term "private" here, since no attribute is really private in Python (without a generally unnecessary amount of work)."

Also:

"If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores."

If you've not read the entire section, I would encourage you to do so.

Update:

To answer the question (now that the title has changed). The pythonic way to use private variables, is to not use private variables. Trying to hide something in python is seldom seen as pythonic.


You can use Python properties instead of getters and setters. Just use an instance attribute and when you need something more complex, make this attribute a property without changing too much code.

http://adam.gomaa.us/blog/2008/aug/11/the-python-property-builtin/


Private variables:

If you use the double underscore at the beginning of your class members they are considered to be private, though not REALLY enforced by python. They simply get some naming tacked on to the front to prevent them from being easily accessed. Single underscore could be treated as "protected".

Getter/Setter:

You can use these if you want to do more to wrap the process and 'protect' your 'private' attributes. But its, again, not required. You could also use Properties, which has getter/setter features.


1) http://docs.python.org/tutorial/classes.html#private-variables

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

(continue reading for more details about class-private variables and name mangling)

2) http://docs.python.org/library/functions.html#property

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜