简体   繁体   中英

Importance of python module constants

While learning about the tkinter module, i came across a few variables like tkinter.TOP , tkinter.BOTTOM , tkinter.NW , etc.

I also learned that tkinter will work equally fine if you replace them with strings such as 'top' , 'bottom' , 'nw' respectively.

I tried to investigate these variables and found that they're just string objects:

>>> import tkinter
>>> tkinter.TOP
'top'
>>> type(tkinter.TOP)
<class 'str'>

My question is, what is the use of these 'module constants' if they can just be replaced by strings? Do they provide some sort of performance improvement? I googled for a while but couldn't find an explanation.

Tkinter provides a tkinter.constants module that contains all the constants (aka. Top , Left , ...). When you import the tkinter module, tkinter.constants is imported automatically.

Usually you can do:

import tkinter
from tkinter.constants import *

print(TOP)

So you don't need to define all the constants that you need to use in your code manually.

import tkinter

TOP = "top"
LEFT = "left"

Plus, there's always a chance that tkinter might want to change some of the constants to something else. (Change TOP to the number 3) Since it's contains less bytes than "top" , so it takes up less space.

Using the constants provided by the module guarantees the constants are always defined properly in the current implementation.

Unless the documentation says you can use either method, then use the constant in the module.

The idea is that the actual implementation of (for example) BOTTOM is hidden so the developers are free to alter it ( encapsulation ). They might, for example, feel that integer 42 would be more efficient to use than the text 'bottom' (and it probably would). That change would break anyone who had make the assumption f the text string.

For the sake of a few characters typed, using the underlying value is not worth the risk.

My recommendation is to never use the tkinter constants. My guess is that they were added to be more pythonic, but in my opinion, it just makes the code harder to read. They provide no performance improvements over using the string literals. It's harmless to use them, though, so it really comes down to personal preference.

Their internal value will likely never change 1 so there's simply no advantage to using the constant over the string literal it represents.

The added benefit to using string literals is that it requires less code. For example, I find this:

something.grid(..., sticky="nsew")

... much preferable to this:

something.grid(..., sticky=tk.N+tk.S+tk.E+tk.W)

It's also worth noting that the official tkinter documentation quite often shows the use of the string literals rather than these constants. For example:


1 Tk -- the library upon which tkinter is built -- takes backwards compatibility very seriously. These constants have not changed in decades . Given that the constant names themselves reflect the actual value of the constant, if the value should ever change then it's almost certain that the name of the constant will change as well. It would be rather odd to have a constant named N be defined as "top" or "upper" or whatever the new value might be.

The thing about such constants for tkinter and python modules in general is, that you can overwrite it manually if needed. This is not only relevant for string constants, but for general config.

I'm not aware of tkinter, but probably they are used somewhere in the code and the developers of tkinter wanted to give you the possibility to simply affect the behaviour via overwriting them.

For example, when using matplotlib , a package for plotting, u can do things like

import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2

to set the linewidth of the plots other than default.

One point is that constants means single copy. Less space used, no GC needed.

Second - compare can be done faster if you know that it is the same object.

Next - code completion - works.

If something is changed - only underlying part needs to be changed - since your code only refers to it, by name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM