0% found this document useful (0 votes)
115 views

What Are Global, Protected and Private Attributes in Python?

Global variables are defined in the global scope and can be accessed inside functions using the global keyword. Protected attributes have a single underscore prefix and can be accessed outside the class but developers should refrain from doing so. Private attributes have a double underscore prefix and result in an AttributeError if accessed from outside the class.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

What Are Global, Protected and Private Attributes in Python?

Global variables are defined in the global scope and can be accessed inside functions using the global keyword. Protected attributes have a single underscore prefix and can be accessed outside the class but developers should refrain from doing so. Private attributes have a double underscore prefix and result in an AttributeError if accessed from outside the class.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

 What are global, protected and private attributes in Python?

• 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 a underscore prefixed to their identifier
eg. _sara. 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 double underscore prefixed to their identifier eg. __ansh.
They cannot be accessed or modified from the outside directly and will result in an
AttributeError if such an attempt is made.

You might also like