How can I take one variable from one function and use it in another function without having to make that variable global?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have basically two choices.
One is to pass it to the second function as a parameter. (If you want the first function to see changes to the value, it needs to be a reference type (e.g. a dict/list) and you have to not overwrite the object, only modify it (e.g.
a.append(b)rather thana = a + [b]).The second is to define a class that can be used as a singleton. Technically, this is still defining something ‘globally’, but it lets you keep things grouped:
(You could also do this with a
dictinstead of a class; matter of preference.)