Reuse function variables

0

I have a script in Python divided into functions and need to access a variable from a function in another example function:

import glob, os, time, datetime, sys, platform
from datetime import datetime
from datetime import timedelta

os.chdir = 'C:\Zabbix\Temp'

#Pegar SO e retorna os valores de acordo com a plataforma: 0 - Linux; 1 - Windows; 2 - ERROR
def getOperationSystem():
    OpSys = platform.system()
    print(OpSys)
    if OpSys == 'Windows':
        return(getParameters('C:\Zabbix\Parameters.txt'))
    elif OpSys == 'Linux':
        return('#')
    else:
        print('Sistema Operacional desconhecido')
        return(2)
        StopIteration()

#Pegar os parametros no arquivo Parameters
def getParameters(dirBkp):
    with open(dirBkp, "r") as arq:
        for linha in arq:
            global values
            values = linha.split(';')
            return getTasks(values[2])

#Valida se o processo do backup está em execução: 0 - Sim; 1 - Não
def getTasks(name):
    read_process = os.popen('tasklist /v').read().strip().split('\n')
    for i in range(len(read_process)):
        process = read_process[i]
        if name in read_process[i]:
            return 0
            StopIteration    
    getTimeArq(values)

#Valor tempo do ultimo arquivo
def getTimeArq(values):

    bkp_name = values[2]
    print("Nome do Bkp:\t\t\t", bkp_name)
    bkp_name = bkp_name+"*.log"
    print(bkp_name)

    log = glob.glob(bkp_name)
    log.sort(key=os.path.getctime, reverse=True)  

    print (log[0])

In this case, getParameters , has a variable that should be used in 3 or more functions of 20 (there is in script )

It loads several values where each function pulls a value, when trying to use it in getTimeArq it returned me this:

    
asked by anonymous 30.07.2018 / 16:42

1 answer

0

Create an object outside the function and pass it as an argument.

Eg:

#!/bin/python3
class fake_global_variables(object):
    nome = ''

def get_dados(memory):
    memory.nome = input('Nome :')
    return ('Que nome legal.')

fake = fake_global_variables()
get_dados(fake)
print(fake.nome)
    
06.08.2018 / 18:50