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

Exploring Other Constants

00:00 Exploring Other Constants in Python Aside from user-defined constants, Python also defines several internal names that can be considered as constants. Some of these names are strict constants, meaning you can’t change them once the interpreter is running.

00:15 This is the case for the __debug__ constant. In these sections of the course, you’ll learn about some internal Python names that you can consider and should treat as constants in your code.

00:27 First, let’s take a look at some built-in constants and their values. According to the Python documentation, a small number of constants live in the built-in namespace.

00:36 The first two constants listed in the docs are True and False, which are the Python Boolean values. These two values are instances of int.

00:49 True has a value of one and False has a value of zero. Note that True and False are strict constants.

00:57 In other words, they can’t be reassigned. If you try to reassign them, then you get a syntax error. These two values are also singleton objects in Python, meaning that there’s only one instance of each.

01:11 That’s why the identity operator is returns True when you compare them with themselves. Another important commonplace constant value is None, which is the null value in Python.

01:23 This constant value comes in handy when you want to express the idea of nullability. Just like True and False, None is also a singleton and strict constant object that can’t be reassigned.

01:39 None is useful as a default argument value in functions, methods, and class constructors. It’s typically used to communicate that the variable is empty.

01:48 Internally, Python uses None as the implicit return value of functions that don’t have an explicit return statement. The ellipsis literal is another constant value in Python.

02:00 This special value is the same as Ellipsis and is the only instance of the types EllipsisType.

02:09 You can use Ellipsis as a placeholder for unwritten code.

02:19 You can also use it to replace the pass statement.

02:35 In type hints, the ellipsis literal communicates the idea of an unknown length collection of data with a uniform type. The

02:46 Ellipsis constant value can come in handy in many situations and help you make your code more readable because of its semantic equivalence to the ellipsis in English.

02:56 Another interesting and potentially useful built constant is __debug__. This is a Boolean constant that defaults to True. It’s a strict constant because you can’t change its value once the interpreter is running.

03:15 The __debug__ constant is closely related to the assert statement. In short, if __debug__ is True, then all the assert statements will run.

03:24 If __debug__ is False, then your assert statements will be disabled and won’t run. This feature can slightly improve the performance of production code.

03:33 To change the value of __debug__ to False, you must run Python in optimize mode by using the -O or -OO command-line options, which provide two levels of bytecode optimization.

03:44 Both levels generate an optimized Python bytecode that doesn’t contain assertions. Even though __debug__ has a dunder name, it’s a strict constant because you can’t change its value once the interpreter is running.

03:56 In contrast, the internal dunder names you’ll see next should be treated as constants but aren’t strict constants. You can change their values during your code’s execution.

04:07 Python also has a broad set of internal dunder names that you can consider as constants. Because there are several of these special names, you’ll just learn about __name__ and __file__ in this course.

04:20 To dive deeper into other dunder names in Python and what they mean to the language, check out the official documentation at the link seen on screen.

04:30 The __name__ attribute is closely related to how you run a given piece of code. When importing a module, Python internally sets __name__ to a string containing the name of the module that you’re importing.

04:42 Create and save the sample_name Python script as seen on screen.

04:53 Once you have this file in place, switch back to the command-line window and run the command seen on screen. With the -c switch you can execute a small piece of Python code at the command line.

05:06 Here you import the sample_name module, which prints some messages to the screen. The first message tells you that __name__ is a string.

05:15 The second message shows that __name__ was set to sample_name, which is the name of the module you just imported. Alternatively, you can run sample_name as a script.

05:28 Python then sets __name__ to __main__. This behavior indicates that you are running the file directly as an executable Python program.

05:40 The __file__ attribute contains the path to the file that Python is currently importing or executing. You can use __file__ from inside a given module when you need to get the path of the module itself.

05:53 Create sample_file.py as seen on screen. You’ll note that it’s very similar to the file you created just now.

06:07 You can check this behavior by running it as seen on screen in a similar way as you ran for sample_name.

06:16 As you can see, Python sets __file__ to contain the path to the module from which you are using or accessing the attribute, and it’s the same regardless of which method you use to run it.

06:29 In the next section of the course, you’ll take a look at other constants, string, math, and type-annotating.

Become a Member to join the conversation.