Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

According to this link:

Like most languages, Python has the concept of private elements:

Private functions, which can't be called from outside their module

If I try to define two files:

#a.py

__num=1

and:

#b.py

import a

print a.__num

At the point when I run b.py, it prints out 1 without giving any exemption. Is diveintopython wrong, or did I misjudge something? Furthermore, is there some approach to do characterize a module's capacity/function as private?

closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer

The link you provided discusses the concept of private elements in Python, such as private functions that are restricted from being called outside their module. However, the example you shared does not demonstrate true encapsulation or privacy in Python.

Python follows a naming convention to indicate that a variable or function is intended to be private. By prefixing the identifier with a single underscore, like _num, it implies that the identifier should be treated as private and not accessed directly.

However, Python does not enforce strict privacy rules or prevent access to private members. In your example, the print a.__num statement successfully prints the value 1 without raising any exception.

It's important to note that using double underscores, like __num, triggers name mangling, which modifies the name to include the class name. This feature is primarily used to avoid naming conflicts in subclasses, but it does not provide complete privacy.

To achieve a similar effect to private functions or variables in Python, it is recommended to follow the naming conventions and use a single underscore prefix to indicate intended internal usage. Although it doesn't restrict access, it serves as a signal to other developers that those elements are intended for internal use and should be accessed with caution from outside the module.

0 votes
by (26.4k points)

In Python, "Privacy" relies upon "consenting adults'" levels of understanding - you can't drive it (anything else than you can, all things considered;- ). A solitary driving underscore implies shouldn't get to it "from an external perspective" - two driving underscores (w/o following underscores) convey the message significantly more strongly

In any case, eventually, it actually relies upon social show and agreement: Python's contemplation is powerful enough that you can't cuff each and every software engineer on the planet to regard your desires.

Interested to learn the concepts of Python in detail? Come and join the python course to gain more knowledge in Python

Watch this video tutorial, if you want to gain more information

0 votes
by (25.7k points)

According to the link you provided, the concept of private elements exists in Python, including private functions that cannot be called from outside their module. However, the example you provided does not demonstrate true encapsulation or privacy in Python.

In Python, there is a naming convention to indicate that a variable or function is intended to be private. By prefixing an identifier with a single underscore, such as _num, it is a signal to other developers that the identifier should be treated as private and not accessed directly.

However, Python does not enforce strict encapsulation or prevent access to private members. It is still possible to access them from outside the module, as demonstrated in your example with print a.__num.

The use of double underscores, like __num, triggers name mangling, which modifies the name to include the class name. This is primarily used to avoid naming conflicts in subclasses but does not provide true privacy.

To achieve a similar effect to private functions or variables in Python, it is recommended to follow the naming conventions and indicate the intended visibility of the elements. By using a single underscore prefix, you communicate to other developers that those elements are intended for internal use and should not be accessed directly from outside the module. However, it is still possible to access them if needed.

0 votes
by (19k points)

The link you shared discusses private elements in Python, like private functions, which are intended to be inaccessible from outside their module. However, Python does not enforce strict privacy rules, and accessing private elements is still possible. By using a single underscore prefix, like _num, you can indicate that an element is intended for internal use. Although it doesn't provide complete privacy, it serves as a signal to other developers. In summary, Python lacks strict privacy enforcement, but naming conventions can help indicate the intended visibility of elements within a module.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...