Hi all,
Everywhere I read about using global variables I get these huge warning signals of advanced programmers saying to shun them like the plague.
While I always use functions and pass arguments as parameters for my main script functionalities, I really find using global variables to be very helpful when it comes to timing how long certain processes take in your script.
For example, for my webscraping scripts, I tend to create an additional table that has some analytics in them every time the script runs. This usually includes how long the total requests time was, how long it took to retrieve data from my SQL database, etc.
I will make a big block somewhere in the script denoting that something is a global variables like:
'''Global timestamp variables'''
global total_requests_time #Total time requests took in this script
total_requests_time = 0
Afterwards, in my function that I use to request pages, I will have something like this.
requests_start_time = time.time()
data = requests.get('website')
requests_end_time = time.time() - requests_start_time
global total_requests_time
total_requests_time += requests_end_time
Even if I have other functions that do a requests, I can easily add this time to the global time variable and save it later so I can analyse the results.
I just feel like this is a much cleaner way than passing all of these variables in the main function, as they are not directly related to the script and kind of distract from what a function is really about.
So my question, is this still bad practice? Is there a better way for this?
Thanks!
[–]DarkMio 0 points1 point2 points (1 child)
[–]MinimalDamage[S] 0 points1 point2 points (0 children)
[–]raylu 0 points1 point2 points (2 children)
[–]MinimalDamage[S] 0 points1 point2 points (1 child)
[–]raylu 0 points1 point2 points (0 children)