Does Python have “private” variables in classes?


There are no 'private variables' in Python classes as such. All variables and methods in Python are public by default. There is sometimes an emulation of private variables by using the double underscore __ prefix to the variable's names. This makes these variables invisible or not easily visible outside of the class that contains them. This is achieved through name mangling. These 'private variables' are not really secure or private because they can be accessed by using certain workaround code. So prefixing the variable names with single underscore _ (semiprivate) and double underscore __ (fully private) makes them difficult to access and not easily visible outside the class which contains them. 

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements