Optimizing variables

The way you select variables in your program can considerably affect the speed of the execution of your program. For instance, if you do not need to change the content or attributes of a widget after its instantiation, do not create a class-wide instance of the widget.

For example, if a Label widget needs to remain static, use Label(root, text='Name').pack(side=LEFT) instead of using the following snippet:

self.mylabel = Label(root, text='Name')
self.mylabel.pack(side=LEFT)

Similarly, do not create local variables if you are not going to use them more than once. For example, use mylabel.config (text= event.keysym) instead of first creating a local variable named key and then using it only once:

key = event.keysym
mylabel.config (text=key)

If the local variable needs to be used more than once, it may make sense to create a local variable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.88