What are global, protected and private attributes in Python?

Last updated 3 weeks, 6 days ago | 21 views 75     5

Tags:- Python

Python | Global, Protected, and Private

Global, Protected, and Private access modifiers in Python. Python does not use these keywords to control access modifications instead single and double underscore  ‘_’ symbols are used to specify Protected and  Private to determine the access control. The variables that are defined in the global scope are Global

 

Global variables are public variables that are defined in the global scope. To use the variable in the global scope inside a function, we use the global keyword.

Protected attributes are attributes defined with an underscore prefixed to their identifier eg. _name. They can still be accessed and modified from outside the class they are defined in but a responsible developer should refrain from doing so.

Private attributes are attributes with a double underscore prefixed to their identifier eg. __role. They cannot be accessed or modified  from the outside directly and will result in an AttributeError if such an attempt is made