/ Python And R Data science skills: 16 Public, Protected and Private Naming Conventions in Python telugu

Saturday 17 February 2018

16 Public, Protected and Private Naming Conventions in Python telugu

16 Public, Protected and Private - Naming Conventions in Python telugu

public just write name

Protected use _name(singel underscore)

Private Use __name

In [19]:
#Public
class car:
    wheels=4
a=car()
print(a.wheels)
4
In [20]:
#Protectd for single calss
class car:
    wheels=4
    _color="red"
a=car()
print(a.wheels)
print(a._color)
4
red
In [23]:
#private for single calss
class car:
    wheels=4
    _color="red"
class car1(car):
    def __init__(self):
        print(self._color)

    

a=car()
print(a.wheels)
#print(a._color)
c=car1()
4
red
In [25]:
class car:
    wheels=4
    _color="red"
    __year=2000
    
a=car()
print(a.wheels)
print(a._color)
print(a.__year)
4
red
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-25-5ad1df7c0d3a> in <module>()
      8 print(a.wheels)
      9 print(a._color)
---> 10 print(a.__year)

AttributeError: 'car' object has no attribute '__year'
In [26]:
#private
#private for single calss
class car:
    wheels=4
    _color="red"
    __year=2000
class car1(car):
    def __init__(self):
        print(self._color)
a=car()
print(a.wheels)
print(a._color)
print(a._car__year)
c=car1()
4
red
2000
red

No comments:

Post a Comment