Global, protected, and private attributes

Question

What are global, protected and private attributes in Python?

in progress 0
Pragya Rani 55 years 3 Answers 1486 views Contributor 0

Answers ( 3 )

  1. Global (also known as public), private, protected attributes are used by a language
    to restrict the access to usage of a class.

    In short, public members can be accessed from anywhere in the class, private members can be
    accessed only from within the class and protected members can be accessed only from within the subclass.
    All members are allowed to be accessed from within the class.

    1) Public attribute
    All members in a Python class are public by default. Any member can be accessed from
    outside the class environment.

    class student:
    def __init__(self, name, id):
    self.name=name
    self.id=id

    >>> s1=student(“swapnil”,100)
    >>> s1.id
    100

    2) Protected
    Python’s convention to make an instance variable protected is to add a
    prefix _ (single underscore) to it. This effectively prevents it to be accessed,
    unless it is from within a sub-class.

    class employee:
    def __init__(self, name, sal):
    self._name=name
    self._salary=sal

    >>> e1=employee(“swapnili”, 10000)
    >>> e1._salary
    10000

    3) Private attributes
    a double underscore __ prefixed to a variable makes it private.
    You cannot access this variable from outside the class. If tried to access, it will throw
    an AttributeError

    class department:
    def __init__(self, name, id):
    self.__name=name # private attribute
    self.__id=id # private attribute

    >>> d1=department(“electronicsl”,101)
    >>> d1.__name
    AttributeError: ‘department’ object has no attribute ‘__name’

  2. There are 3 types of access modifiers for a class in Python. These access modifiers define how the members of the class can be accessed. Of course, any member of a class is accessible inside any member function of that same class

    Access Modifier: Public
    The members declared as Public are accessible from outside the Class through an object of the class.

    Access Modifier: Protected
    The members declared as Protected are accessible from outside the class but only in a class derived from it that is in the child or subclass.

    Access Modifier: Private
    These members are only accessible from within the class. No outside Access is allowed.

  3. 1) The access modifiers in Python are used to modify the default scope of variables. There are three types of access modifiers in Python: public, private, and protected.

    2) Variables with the public access modifiers can be accessed anywhere inside or outside the class, the private variables can only be accessed inside the class, while protected variables can be accessed within the same package.

    3) To create a private variable, you need to prefix double underscores with the name of the variable. To create a protected variable, you need to prefix a single underscore with the variable name. For public variables, you do not have to add any prefixes at all.

    4) Example:
    class Car:
    def __init__(self):
    print (“Engine started”)
    self.name = “corolla” #public
    self.__make = “toyota” #private
    self._model = 1999. #protected

Leave an answer

Browse
Browse