Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resetting Global Variables?
#11
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#12
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,122 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,029 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,151 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 949 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  resetting an iterator to full Skaperen 7 6,981 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,937 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Global variables not working hobbyist 9 4,739 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,655 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,264 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 5,105 Apr-12-2020, 03:58 PM
Last Post: TomToad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020