Section | Video Links |
---|---|
Memento Overview | |
Memento Use Case | |
Getters/Setters |
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
python ./memento/memento_concept.py
Originator: Setting state to `State #1`
Originator: Setting state to `State #2`
CareTaker: Getting a copy of Originators current state
Originator: Providing Memento of state to caretaker.
Originator: Setting state to `State #3`
CareTaker: Getting a copy of Originators current state
Originator: Providing Memento of state to caretaker.
Originator: Setting state to `State #4`
State #4
CareTaker: Restoring Originators state from Memento
Originator: State after restoring from Memento: `State #2`
State #2
CareTaker: Restoring Originators state from Memento
Originator: State after restoring from Memento: `State #3`
State #3
... Refer to Book or Design Patterns In Python website to read textual content.
python ./memento/client.py
Score: 200, Level: 0, Location: {'x': 0, 'y': 0, 'z': 2}
Inventory: {'rifle', 'sword'}
CareTaker: Game Save
Score: 500, Level: 1, Location: {'x': 0, 'y': 0, 'z': 13}
Inventory: {'motorbike', 'rifle', 'sword'}
CareTaker: Game Save
Score: 600, Level: 2, Location: {'x': 0, 'y': 0, 'z': 14}
Inventory: {'motorbike', 'rifle', 'sword'}
CareTaker: Restoring Characters attributes from Memento
Score: 200, Level: 0, Location: {'x': 0, 'y': 0, 'z': 2}
Inventory: {'rifle', 'sword'}
Often when coding attributes in classes, you may want to provide methods to allow external functions to read or modify a classes internal attributes.
A common approach would be to add two methods prefixed with get_
and set_
,
class ExampleClass:
def __init__(self):
self._value = 123
def get_value(self):
return self._value
def set_value(self, value):
self._value = value
example = ExampleClass()
print(example.get_value())
This makes perfect sense what the intentions are, but there is a more pythonic way of doing this and that is by using the inbuilt Python @property
decorator.
class ExampleClass:
def __init__(self):
self._value = 123
@property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value
example = ExampleClass()
print(example.value)
Note that in the above example, there is an extra decorator named @value.setter
. This is used for setting the _value
attribute.
Along with the above two new getter/setter methods, there is also another method for deleting an attribute called deleter
.
class ExampleClass:
def __init__(self):
self._value = 123
@property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value
@value.deleter
def value(self):
print('Deleting _value')
del self._value
example = ExampleClass()
print(example.value)
del example.value
print(example.value) # now raises an AttributeError
... Refer to Book or Design Patterns In Python website to read textual content.