Saturday, July 6, 2019

Easy Way To Avoid Global Vars

The Easy Way To Avoid Using Global Variables

A Step By Step Guide

python-easy-way-to-avoid-global-variables-logo

I am currently still working on an update of Screen Spy, but I just had to break off that work to report this finding, because it will probably be of use in most of my future projects.

This is newb important stuff people, because I am about to describe how to avoid ever needing to use a global statement in your code ever again, (probably).

Why are globals bad?

"This has nothing to do with Python; global variables 
are evil in any programming language."

Because the experts say so, and it is a right giveaway that one is a noob coder. As soon as a pro coder spots a global statement his nose will usually turn up a little, as if he has caught a faint whiff of a turd, and she would be right, I suppose.

Towing The Line

Personally I don’t see what all the fuss is about. They say globals variables can hide bugs that can be difficult to track down. Ahem! My code is always so riddled with mistakes I am kind of an expert in finding bugs now! So that doesn’t bother me much.

To be fair, the pro’s are probably talking about production code of huge length though, my projects rarely go above a few hundred lines of code.

Anyway, I do like to try and tow the line if at all possible, and I will always try to simplify most things for myself if I can, and that is what has happened here.

I did use a version of this method in my Bletchley code, but when I went back over that code it was incomprehensible to me as somehow it got all complex when it was refactored for me by an expert coder, bless him.

Cuddly?

But I knew the principle of how this method worked. It took a few hours to simplify it, and get it down to a five year old’s level, so I that could understand it and use it again myself.

This article will now be my reference should I need it in my future projects, you can use it too. I’m so good to you guys.

Nine Globals To Nil

In my Screen Spy code I had seven functions and each function had two or three global statements in them. Some variables were in more than one function, but it still came to nine different globals that’s a lot for about 100 lines of code.

Those Globals Gotta Go

I know that to completely finish off Screen Spy I am going to need help with a Linux issue to make it multi-platform. Now Linux pro coders are the worst, they are so damn smart they are barely intelligible to a Py-Wally such as myself, and they might be looking at my code,  so those globals simply had to go to save what little self respect I have left.

Ridding Your Code Of Globals;

A Step By Step Guide

1: Use a backup copy of your code, in case you balls it up.

2: Now gather together all the global variables that you use in your program and set them to their starting values. As an example I will use the nine globals from screen spy code.

sys_platform = “”
save_folder = “screens/”
save_on = True
stop_thread = False
file_name = “”
file_inc = 0
image_count = 0
grab_secs = 2
max_images = 7200

3: Now remove all the global statements from your code, or comment them out for now, if you prefer.

4. Put the above list of variables into this very simplified class. I can even understand this class so don’t fret if you are scared of classes, this is no different to a function really, (syntax wise anyway).

class glo:
'''global store, this makes these vars global,e.g glo.var'''
    sys_platform = ""
    save_folder = "screens/"
    save_on = True
    stop_thread = False
    file_name = ""
    file_inc = 0
    image_count = 0
    grab_secs = 2
    max_images = 7200

5. Put this class just after your imports at the beginning of your code for easy reference.

6. Now you are going to have to prefix every occurrence of your (previously global) variables with the name of your class and a dot. In this example I called the class “glo

So what used to be this:

def toggle_save():
    '''Toggles save an image or not when Shift+Ctrl+1 pressed.'''
global save_on
    save_on = not save_on

Now becomes this:

def toggle_save():
    '''Toggles save an image or not when Shift+Ctrl+1 pressed.'''
    glo.save_on = not glo.save_on

All I did was remove the global statement and add glo. to the save_on variable.

And that is it.

Once you have renamed all your now-ex-globals, all your variables are now stored neatly in a class and can be accessed anywhere in your code using, yourclassname.yourvariable.

I hope that makes sense?

I originally put a call to the class glo(), which worked, but it also works without the call, so I’m not sure about that yet. I’ll get back to you on that one. My knowledge of classes suck big bottom as you can see. We live and learn though.

My next article should be another screen spy update, so you can see the class in action there, but until then, be good, if not then be dirty.


Using Python V3.6.5 32bit, on Windows 7 64bit

and Python V3.67 64bit on Linux Mint 19.1 Cinnamon 64bit

Home Page

Previous Post: Python Code Snippets #23


 

 

Advertisements


from Python Coder
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...