python property and attribute code example
Example 1: @property in python
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
print('Getting name')
return self._name
@name.setter
def name(self, value):
print('Setting name to ' + value)
self._name = value
@name.deleter
def name(self):
print('Deleting name')
del self._name
p = Person('Adam')
print('The name is:', p.name)
p.name = 'John'
del p.name
Example 2: attributes in python
class Monkey(object):
def __init__(self, name):
self.name = name
def speak(self):
print("Hello! I am " + self.name)
IronMan = Monkey('TonyStark')
IronMan.speak()