K
K
Kurum2017-03-10 21:07:00
Python
Kurum, 2017-03-10 21:07:00

How can I refer to a variable in another function in a function?

Python 3. PyQt4
Conditional question model:
There is a function "aa" with variables. I want to use these variables in the "bb" function like this:

def aa(self):
    x = 3
    y = 5
    z = 6
def bb(self):
    s = aa.x + 5
    print('s = ',aa.x,'+ 5 = ',s)
bb()

Both functions belong to the QWidget class.
In real life, my question looks like this:
def on_kat(self):
        кат_index = self.listWidget_кат.currentRow() # принимает номер строки
        уч_lst = basa.кат[кат_index+1]
        self.listWidget_уч.clear()
        self.listWidget_уч.addItems(уч_lst)
        return кат_index 
def on_kat_minus(self, кат_index):
        кат_index = on_kat.кат_index
        print('кат_index = ',кат_index)

Those. in the second function, you need to use the variable from the first function - cat_index

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-03-10
@Pyrym

In this form, no way.
Variables declared inside a function exist only for that function and only for the duration of its execution.
Accordingly, it is necessary either to move the declaration of variables outside the function - to some namespace common to both functions. Either return variables as the result of a function, and call that function to get its result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question