What is good practice way of data hiding in python? [duplicate] - python

This question already has answers here:
What is the meaning of single and double underscore before an object name?
(18 answers)
Closed 4 years ago.
What is good practice way of data hiding in python ? Is it the same as java ?
private is with double underscore ?
protected is with single underscore ?
public function is possible, member public is not recommended ?
Right ?

The concept of data hiding does not really exist in python.
From the docs:
“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.

Related

Is it possible to have protected class variables or methods in python? [duplicate]

This question already has answers here:
Does Python have “private” variables in classes?
(15 answers)
Closed 3 years ago.
Is it possible to have protected class variables or methods in python? Can I see an example of such usage?
The short answer is "no." There are conventions and good style that allow you to indicate that someone shouldn't be modifying those variables or calling those methods from outside the class but there is no way to strictly enforce this. There essentially is no such thing as strictly enforced private or protected variables or methods in Python.
See this tutorial.
No it is not possible. People generally use underscores as a convention for private members.
This question on the general python convention can give some more information.
Python "private" function coding convention
Basically putting a '_' before your member name will indicate to the outside world that it is private.
not_private = 0
_private = 1

Golang equivalent of pythons __getattr__() or __call__() [duplicate]

This question already has answers here:
Create a struct by reflection in Go
(1 answer)
Is it possible to dynamically create a function with a receiver (method) in go?
(1 answer)
Can I create a new function using reflection in Go?
(4 answers)
Closed 8 months ago.
I would like to manipulate structs at Runtime.
For example, I have a struct:
type Item struct {
SomeField string
}
Is it possible to add field on runtime? Or Access Attribute that is not yet defined. Something like pythons __getattr__() or __call__() so I could dynamically control the fields/methods accessed.
E.g. do something like
Item.DynamicField or Item.DynamicMethod() where I don't know exactly the Field or the Method that will be accessed/called, So I can't define it statically.
Maybe I'm missing something in the Reflect package?
Thank you.
https://github.com/oleiade/reflections
The purpose of reflections package is to make developers life easier
when it comes to introspect structures at runtime. Its API is inspired
from python language (getattr, setattr, hasattr...) and provides
a simplified access to structure fields and tags.
Is it possible to add field on runtime? Or Access Attribute that is not yet defined.
No. Go is a compiled language with statically defined types. You probably need a map if you want to dynamically add properties.

What does the underscore mean in python [duplicate]

This question already has answers here:
Why does python use two underscores for certain things? [duplicate]
(7 answers)
Closed 9 years ago.
Hi everyone as it obvious from my question I am like a brand new to python.
I am so confused when I am reading the documentation on python or even here in the Stackoverflow forum...
Why do they write like that
from __future__ import division
What does the underscore around the Future word mean ?? And Are we supposed to use it like that with the underscore in the python interpreter ?
This is just one of tons of examples. Any help would be greatly appericated.
According to PEP 236 where this module was proposed, the double underscores make it a reserved name.
[5] This ensures that a future_statement run under a release prior to
the first one in which a given feature is known (but >= 2.1) will
raise a compile-time error rather than silently do a wrong thing.
If transported to a release prior to 2.1, a runtime error will be
raised because of the failure to import __future__ (no such module
existed in the standard distribution before the 2.1 release, and
the double underscores make it a reserved name).

What are the access specifiers in python? [duplicate]

This question already has answers here:
Does Python have “private” variables in classes?
(15 answers)
Closed 9 years ago.
I have come from c++ and java background so, I was curious to know if python provides access specifiers as provided by c++/java. I've seen some code, and this is what I think,
__variable ---> private.
_variable ----> protected.
Correct me if I'm wrong.
Python has recommended practices rather than prescriptive ones - so anything with a _ at the start should be left alone by others but is not locked to prevent it. There is however name mangling to make life more interesting for members with a __ at the start - see PEP8.
Of course if others rely on your private data/methods rather then the public API they only have themselves to blame when you change something and their code stops working.
There is no such concept in Python. There are conventions that are used - like the ones Steve mentioned but also others such as calling the first variable of an instance method self.
In addition, for module level imports - there is a way to prevent the default behavior of importing all names from a module. This is done by populating __all__ with a list of names that should be imported (exposed) by default.
However, as with __var and _var it is just a convention (although one that is enforced by Python). It doesn't restrict you though - you can explicitly import any name.

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 query.
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.
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

Categories