Constant and frozen variables in python
- Wait what, constants in Python?
- A space of constants (SpaceConstants)
- A space of frozen values (SpaceFrozenValues)
- A constant space (ConstantSpace)
- A frozen space (FrozenSpace)
- References
Wait what, constants in Python?
This post in the result a question I found in StackOverflow and some research I was doing about Python idioms. The problem is basically how do I create a constant in Python?.1
In principle, this is not possible because as Alex Martelli put it in Python any variable can be re-bound at will.2 This impossibility is a feature of the language and not a limitation. The most accepted pythonic way of having a constant is by name convention. Once developers agree upon a way to designate a constant they just do not change it.
However, Python is such a dynamic language that allows the creation of a construct that looks and feels as a constant. This blog is to explore what can be expressed by the language. Likely, you do not need these idioms, please be careful and do not attempt this at home!
In this post, I will call a constant variable to a constant reference to values (immutable or otherwise). Moreover, I say that a variable has a frozen value when it references a mutable object that a client-code cannot update its value(s).
Now, without further ado, here are the idioms.
A space of constants (SpaceConstants)
This idiom creates what looks like a namespace of constant variables (a.k.a. SpaceConstants). It is a modification of a code snippet by Alex2 to avoid the use of module objects. In particular, this modification uses what I call a class factory because within SpaceConstants function, a class called SpaceConstants is defined, and an instance of it is returned.
I explored the use of class factory to implement a policy-based design look-alike in Python in stackoverflow3 and also in a blog4.
A space of frozen values (SpaceFrozenValues)
This next idiom is a modification of the SpaceConstants in where referenced mutable objects are frozen. This implementation exploits what I call shared closure between setattr and getattr functions5. The value of the mutable object is copy and referenced by variable cache define inside of the function shared closure. It forms what I call a closure protected copy of a mutable object6.
You must be careful in using this idiom because getattr return the value of cache by doing a deep copy. This operation could have a significant performance impact on large objects!
A constant space (ConstantSpace)
This idiom is an immutable namespace of constant variables or ConstantSpace. It is a combination of awesomely simple Jon Betts’ answer in stackoverflow7 with a class factory3.
A frozen space (FrozenSpace)
This idiom is an immutable namespace of frozen variables or FrozenSpace. It is derived from the previous pattern by making each variable a protected property by closure6 of the generated FrozenSpace class.
References
Note of a interdiciplinarian by Victor E. Bazterra is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.