Python Forum
Resetting Global Variables? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Resetting Global Variables? (/thread-13117.html)

Pages: 1 2


RE: Resetting Global Variables? - volcano63 - Sep-28-2018

Use OOP, with properly designed classes.

And write according to PEP-8 - because otherwise quite a few Pythonista (myself included) will refuse looking at your code


RE: Resetting Global Variables? - WuchaDoin - Sep-28-2018

(Sep-28-2018, 08:34 PM)ichabod801 Wrote: I'm going to back down to simple things, and rewrite your three function example with parameters:

def Main(CustomerIndex, LoadedSales, InxLoc):
    CustomerSaleID = CustomerSaleOBJID(LoadedSales, InxLoc)
    Vin = FindVin(CustomerIndex, LoadedSales, InxLoc)
    Fnum = FindFnum(CustomerIndex, CustomerSaleID)

def FindVin(CustomerIndex, LoadedSales, InxLoc):
    Vin = str(CustomerIndex[int(float(LoadedSales['CC'][InxLoc]))][2])[-8:]
    return Vin
 
def FindFnum(CustomerIndex, CustomerSaleID):
    Fnum = str(CustomerIndex[CustomerSaleID][3])
    return Fnum
 
def CustomerSaleOBJID(LoadedSales, InxLoc):
    CustomerSaleID = int(float(LoadedSales['CC'][InxLoc]))
    return CustomerSaleID
So each value that a function needs is passed to that function as a parameter when it is called. To do this, you have to specify that the function is expecting that parameter when it is defined. You say this Fnum is needed in 13 different functions. So each one of those would be defined with Fnum as a parameter, and when each one is called, Fnum would get passed to that function as a parameter. All of this is explained in more detail in the functions tutorial linked to in my signature below. Including what Nilamo alluded to, where you can assign default values to parameters, so that the function can be called without that parameter, if it is a common (but not always) used parameter.

Oh wow. Seeing it written that way makes much more sense! Now I can see why that is more appropriate than using outright global variables. All the help is very much appreciated and saved me so much time. I will have to go back through it all to re-arrange it and setup the functions appropriately, but this will be of great use in the future. Again, much appreciated.

(Sep-28-2018, 08:36 PM)volcano63 Wrote: Use OOP, with properly designed classes.

And write according to PEP-8 - because otherwise quite a few Pythonista (myself included) will refuse looking at your code

I am 4 weeks new to Python. Never looked into it before hand. I began just by writing code; I never considered object orientation or any type of layout other than that I wanted it to look at least structured. But now that I am more familiar with the basics, I can see how object orientation is almost always sought(imho). I appreciate your response as well. Anything help because sometimes it can actually take while to get responses. You just saved me from asking another question in the future.