Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resetting Global Variables?
#5
(Sep-28-2018, 05:14 PM)ichabod801 Wrote:
(Sep-28-2018, 04:35 PM)WuchaDoin Wrote: How would you use a variable globally without being global?

With parameters and return values:

>>>> def add2(x):
...     return x + 2
...
>>> z = 5
>>> z = add2(z)
>>> z
7
Also with classes, which can retain values within an instance of the class. There are links to tutorials for both in my signature below.

With posting this example, I've noticed the problem I keep encountering is that looks like it calls for global variables. Here is how it is setup and why I am having to use global variables. I know there is a work around which is why I am posting the snippet.

First I have main called:
            def Main():
                CustomerSaleID = CustomerSaleOBJID()
                Vin = FindVin()
                FPnum = FindFPnum()
Then each function is called in the proper order in Main().
def FindVin():
    Vin = str(CustomerIndex[int(float(LoadedSales['CC'][InxLoc]))][2])[-8:]
    return Vin

def FindFnum():
    Fnum = str(CustomerIndex[CustomerSaleID][3])
    return Fnum

def CustomerSaleOBJID():
    CustomerSaleID = int(float(LoadedSales['CC'][InxLoc]))
    return CustomerSaleID
When the code reaches FindFnum() and has to use the variable CustomerSaleID (which should already be declared by then); This is where I begin to encounter my problem. I need to use CustomerSaleID in multiple functions, but using what it shown above; it seems to create a local variable? Vin, CustomerSaleID, and FPnum will be use throughout all 13 functions. Would the workaround being that I would have to declare a local variable to be used within each function?
i.e.
def FindFnum():
    CustomerSaleID = CustomerSaleOBJID()
    Fnum = str(CustomerIndex[CustomerSaleID][3])
    return Fnum
Reply


Messages In This Thread
Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 03:12 PM
RE: Resetting Global Variables? - by micseydel - Sep-28-2018, 04:31 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 04:35 PM
RE: Resetting Global Variables? - by ichabod801 - Sep-28-2018, 05:14 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 05:20 PM
RE: Resetting Global Variables? - by nilamo - Sep-28-2018, 06:41 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 06:59 PM
RE: Resetting Global Variables? - by nilamo - Sep-28-2018, 07:09 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 07:50 PM
RE: Resetting Global Variables? - by ichabod801 - Sep-28-2018, 08:34 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 08:41 PM
RE: Resetting Global Variables? - by volcano63 - Sep-28-2018, 08:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,190 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,072 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,188 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 989 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  resetting an iterator to full Skaperen 7 7,129 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,973 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Global variables not working hobbyist 9 4,819 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,702 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,324 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 5,205 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