You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
973 B
47 lines
973 B
class TestThis(object): |
|
'''Demonstrates how to use property() as a decorator |
|
The power of this will be in declaring essentially private variables |
|
Property has built in property.setter, property.getter, property.deleter''' |
|
def __init__(self): |
|
self.voltage = 0 |
|
|
|
@property |
|
def voltage(self): |
|
return self._voltage |
|
|
|
@voltage.setter |
|
def voltage(self, value): |
|
'''Variables inherit name of function, clearly. Can place tests on things here, if desired''' |
|
if value < 0: |
|
print('Voltage can not be negative') |
|
self._voltage=0 |
|
else: |
|
self._voltage = value |
|
|
|
@voltage.deleter |
|
def voltage(self): |
|
print('Deleting voltage') |
|
del self._voltage |
|
|
|
@property |
|
def higher(self): |
|
self._higher = self.voltage*3 |
|
return self._higher |
|
|
|
@higher.deleter |
|
def higher(self): |
|
print('deleted higher') |
|
del self._higher |
|
|
|
if __name__=='__main__': |
|
x = TestThis() |
|
x.voltage = -217 |
|
print(x.voltage) |
|
print(x.higher) |
|
x.voltage = 3 |
|
print(x.higher) |
|
del x.higher |
|
|
|
|
|
|
|
|
|
|