Home > Software design >  Get a Variable directly from function Python
Get a Variable directly from function Python

Time:11-16

i want to work with the variable "sets" after the function has been called.

How can i do this?

sets = 0
  
def search_str(monatsabrechnung, d1):
       with open(monatsabrechnung, 'r') as file:
           
           content = file.read()
   
           if lastDay == heute and ja == 1:
               sets = 1         
           
           else:
               pass
               

search_str(monatsabrechnung, d1)


print(sets)

CodePudding user response:

IIUC, you are trying to modify a variable in a function, which is originally defined outside a function. This is a variable scoping problem. Do check this awesome article to get an understanding of how variable scopes work in python.

Back to your code, the issue here is that even though you run the function to modify the variable sets to 1, python goes back to the outer scope where sets were set to 0.

sets = 0             #<-- outer scope
def search_str():
    ...              #do something
    sets = 1         #<-- inner scope              

search_str()
print(sets)          #back to outer scope

# 0

Solution 1: Pass as a parameter and return

You will have to pass the variable as a parameter to the function and then return it as follows -

sets = 0
def search_str(sets):        #<-- pass as paramter
    ...                      #do something
    sets = 1                 #<-- modify
    return sets         

output = search_str(sets)    #<-- save the output
print(output)

# 1

TIP: If your function is already returning another output, you can actually return, store and unpack multiple return values at once -

  1. In your return statement, return everything you need return x, y, sets
  2. Then, while calling use - X, Y, sets = search_str(...)

Solution 2: Set scope to global

If passing a parameter and returning is not an option for you, the scope of the variable has to be made global -

sets = 0
def search_str():
    global sets           #<-- set the scope to global
    ...                   #do something
    sets = 1
               
search_str()
print(sets)

# 1

EDIT: As an additional pointer, as Matthias points out in his comments correctly, global scoping is usually avoided as it can cause a lot of problems if not careful.

Here is a great StackOverflow thread detailing Why are global variables evil?

CodePudding user response:

Return the new value of sets from the function. If the if statement is true, then return 1, otherwise return the same value as before. To be able to return the old value you should add it as a parameter to the function.

sets = 0

def search_str(monatsabrechnung, d1, sets):
    with open(monatsabrechnung, 'r') as file:
        content = file.read()
        if lastDay == heute and ja == 1:
            current_sets = 1
        else:
            current_sets = sets
    return current_sets

sets = search_str(monatsabrechnung, d1, sets)

Don't use global if you don't have to. You get side-effects and the code is less reusable.

  • Related