Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Defining Strict Constants

00:00 Defining Strict Constants in Python Up to this point, you’ve learned a lot about programming and Python constants. You now know that Python doesn’t support strict constants, it just has variables.

00:12 So the Python community has adopted a naming convention of using uppercase letters to communicate that a given variable is really a constant. So in Python, you don’t have constants, but you have variables that never change.

00:24 This can be an issue if you’re working on a large Python project with many programmers at different levels. In this situation, it would be good to have a mechanism that guarantees strict constants, constants that no one can change after the program has started.

00:40 Because Python is a flexible programming language, you’ll find several ways to achieve that goal of making a constant unchangeable. They all imply creating a custom class and using it as a namespace for constants.

00:53 But why should you use a class as a namespace for your constants? Well, in Python, any name can be rebound at will. At the module level, you don’t have the appropriate tools to prevent this from happening.

01:04 So you need to use a class because classes provide more customization tools than modules. Here you’ll learn several ways to use your class as your namespace for strict constants, Python classes allow you to define a special class attribute called __slots__.

01:21 This attribute will hold a sequence of names that will work as instance attributes. You won’t be able to add a new instance attribute to a class with a __slots__ attribute, because __slots__ prevents the creation of an instance __dict__ attribute.

01:36 Additionally, not having this attribute implies an optimization in terms of memory consumption. Using __slots__, you can create a class that works as a namespace for read-only constants.

01:50 Here you define ConstantsNamespace. The class’s __slots__ attribute holds an empty tuple, meaning that instances of this class will have no attributes.

02:00 You then define your constants as class attributes.

02:08 The next step is to instantiate the class to create a variable holding the namespace with all the constants.

02:19 Note, you can now quickly access any constant in your special namespace, but you can’t assign this a new value. If you try to, you’ll get an attribute error.

02:31 With this technique, you are guaranteeing that no one else on your team can change the value of the constants. You’ve achieved the expected behavior of a strict constant.

02:42 You can also take advantage of the property decorator to create a class that works as a namespace for constants. To do this, you just need to define your constants as properties without providing them with a setter method.

03:14 Because you don’t provide setter methods for the PI and Euler number properties, they are read-only. This means that you can only access their values. It’s impossible to assign a new value to either one.

03:26 If you try to do it, then you get an attribute error.

03:32 Python’s collections module provides a factory function called namedtuple(). This function lets you create tuple subclasses that allow the use of named fields and dot notation to access their items.

03:44 Like regular tuple, namedtuple() instances are immutable, which implies that you can’t modify an existing namedtuple() object in place.

03:53 Being immutable sounds appropriate for creating a class that works as a namespace of strict constants and here’s how to do it.

04:08 Here, the constants play the roles of fields in the underlying namedtuple, ConstantsNamespace.

04:19 Once you’ve created the namedtuple() instance constants, you can access your constants using the dot notation.

04:29 Because tuples are immutable, there’s no way to modify the value of any field. So the constants namedtuple() object is a full-fledged namespace of strict constants.

04:43 Data classes are classes that contain mainly data, as their name indicates. They can also have methods, but that’s not their primary goal. To create a data class, you need to use the data class decorator from the dataclasses module.

04:57 But how can you use this type of class to create a namespace of strict constants? The data class decorator accepts a frozen argument that allows you to mark your data class as immutable.

05:08 If it’s immutable then once you’ve created an instance of a given data class, you have no way to modify its instance attributes. On screen, you can see how to use a data class to create a namespace containing your constants.

05:23 Here, you first import the data class decorator. Then you use this decorator to turn ConstantsNamespace into a data class. To make the data class immutable, you set the frozen argument to True.

05:36 Finally, you define ConstantsNamespace with your constants as class attributes.

05:46 You can create an instance of this class and use it as your ConstantsNamespace.

05:57 Again, you can access all the constants, but you can’t modify their values because the data class is frozen.

06:07 Python classes let you define a special method called __setattr__. This method allows you to customize the attribute assignment process because Python automatically calls the method on every attribute assignment.

06:20 In practice, you can override __setattr__ to prevent all attribute reassignments and make your attributes immutable. On screen you can see how to do this.

06:41 The custom implementation of __setattr__ doesn’t perform any assignment operation on the class’s attributes. Instead, it raises an attribute error when you try to set an attribute.

06:52 This implementation makes the attributes immutable.

07:02 Once more, the ConstantsNamespace behaves as a namespace for constants.

07:09 So having seen a number of ways to define strict constants in Python, in the next section of the course, you’ll take a look back at what you’ve learned.

Become a Member to join the conversation.