Python - Encapsulation and Data Protection
What will be the output of this code?
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
p = Person('Alice')
print(p.name)What will be the output of this code?
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
p = Person('Alice')
print(p.name)name method is decorated with @property, so p.name accesses the method and returns self._name>.p._name is set to 'Alice', printing p.name outputs 'Alice'.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions