brennerm / PyTricks

Collection of less popular features and tricks for the Python programming language
MIT License
3.07k stars 502 forks source link

Make this example more meaningful/fix bug #107

Closed singhbgithub-compass closed 4 years ago

singhbgithub-compass commented 4 years ago

By doing:

@contextlib.contextmanager
def unlock(resource):
    resource = 'unlocked'
    ...

We don't really change the resource that is passed in. Because Python is pass by object reference, doing a re-assignment operation does not actually change the resource i.e. resource = 'unlocked' does not really work. Furthermore, the resource variable, with which the unlock function is called, is a string; strings are immutable; thus, we need to pass in a mutable data type as the resource to be able to demonstrate the resource's "lock state" changing. While we could have used a simple "box" tactic i.e. resource = ['locked'], this seemed to distract from the goal of the example. Consequently, it seemed using a small class might be clearer without detracting from the example's value.