Answers for "attribute privé python"

1

attribute privé python

class point:
    def __init__(self,a,b):
        self._x=a 
        self.y= b

p=point(1,2)
print(p.y)
#l'attribut y de l'objet p est par defaut publique 
print(p.x)
#l'attribut x est privé(_x) donc on ne peut acceder directement a sa valeur au dehors de la classe

#output --------------------------------------->

2


Traceback (most recent call last):
  File "file0.py", line 9, in <module>
    print(p.x)
AttributeError: 'point' object has no attribute 'x'
Posted by: Guest on July-17-2021

Python Answers by Framework

Browse Popular Code Answers by Language