How to access variables outside of functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to access variables outside of functions

I have not learned some lessons via SL yet , and I strongly need to know how to access lists , sets , and any type of variable , outside of a function . Please let me know that

4th Jun 2021, 1:33 PM
Ali_combination
Ali_combination - avatar
21 Answers
+ 5
before taking a decision, i would like to see your code please. place the code in playground and link it here. thanks!
4th Jun 2021, 2:21 PM
Lothar
Lothar - avatar
+ 4
make them global.
4th Jun 2021, 1:47 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 2
Use global ,for eg you have local veriable called "list1" to make it global use "global list1" inside func. Simple🙂👍
4th Jun 2021, 2:16 PM
Yash Wable 🇮🇳
Yash Wable 🇮🇳 - avatar
+ 2
😊😊👍 actually it was Akshay code I just edited that code bite for showing simple way
5th Jun 2021, 3:39 AM
Yash Wable 🇮🇳
Yash Wable 🇮🇳 - avatar
+ 2
global
5th Jun 2021, 11:19 PM
H Ξ X V G 0 И
H Ξ X V G 0 И - avatar
+ 2
You can return variable, make them static to global
6th Jun 2021, 9:50 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 2
Ali_combination not is multiple returns it's returning a tuple and using unpacking to get each value when invoke. You can also return a wrapped object that accomplish some own model class or other data structure It's more a problem of software engineering to apply patterns in the right way. Eg. About recursion using decorators https://code.sololearn.com/ct0FMgR963HC/?ref=app in the end, it all comes down to having a stop condition (the base case) and passing the following values ​​to the same function (nexts steps) so, that on the stack backwards phase, the function itself does the merge. No rare, extra, global, etc... variables are needed a concurrent recursion could be seen in any quicksort algorithm (divide and conquer strategy), splitting the iterable to sort into chunks recursively until some size threshold (each one can be sorted in a threadpool) and make a final sort of all chunks when each one is done. I confirm you for sure that increases performance when you have to process tons of data.
6th Jun 2021, 11:56 AM
David Ordás
David Ordás - avatar
+ 1
Lothar of course , I was solving a problem in a contest , and I faced the problem ... now the contest is finished but I saved the code . Please wait for a while , then I rewrite the code and will send it to you 👍
4th Jun 2021, 2:30 PM
Ali_combination
Ali_combination - avatar
+ 1
Lothar Hello gentelman , I tried to use your username to tag you on my last question I discuss , but it didnot work .. anyway , sorry for posting it late . I was rewriting that ..
4th Jun 2021, 7:28 PM
Ali_combination
Ali_combination - avatar
+ 1
def f(): global a a = "Hello" #Simple Variable
5th Jun 2021, 3:37 AM
Yash Wable 🇮🇳
Yash Wable 🇮🇳 - avatar
+ 1
Using global variables not are the best option, eg in a concurrent environment using threads or processes because you get side effects. Rellot's screwdriver H Ξ X V G 0 И Neither polute function object AKSHAY🇮🇳 Yash Wable 🇮🇳 Ali_combination Why you don't returns these variables from function? It's easy and a good coding practice (or even the best). def foo(a, b): c = a * b return a*a, b*b, c aa, bb, c = foo(4, 5) print(aa, bb, c)
6th Jun 2021, 3:38 AM
David Ordás
David Ordás - avatar
+ 1
Solving recursion using an abstract and generic algorithm to any problem. (Defs.: Software pattern) https://code.sololearn.com/ct0FMgR963HC/?ref=app
6th Jun 2021, 12:42 PM
David Ordás
David Ordás - avatar
0
Rellot's screwdriver thank you so much gentleman , think its d best way to do it , compared to the ways that I saw on google
4th Jun 2021, 2:27 PM
Ali_combination
Ali_combination - avatar
0
Yash Wable 🇮🇳 got it ! Thanks !❤🙌
4th Jun 2021, 2:28 PM
Ali_combination
Ali_combination - avatar
5th Jun 2021, 3:28 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
0
Yash Wable 🇮🇳 Yep , thats a good way too .. thank you 😊👍
5th Jun 2021, 3:38 AM
Ali_combination
Ali_combination - avatar
0
David Ordás Umm ... I can see something new here 🤔 I did not know that in Python , we can return multi values ... thanks ! But sometimes we need to access external variables . For example when using recrusion , so it starts a new process while I need to increment a variable and use that in some situations . I do not always need to return it , actually I need some variables to return some other , by using external ones ...
6th Jun 2021, 6:26 AM
Ali_combination
Ali_combination - avatar
0
David Ordás I still have not studied all lessons in Python ... I guess I'd find better ways to do that . What do you mean by "It's more a problem of software engineering to APPLY PATTERNS IN THE RIGHT WAY" ? I think I do not know anything about it .. please send me a source to read about it . Thanks .
6th Jun 2021, 12:05 PM
Ali_combination
Ali_combination - avatar
0
David Ordás thanks for your precious reply !
6th Jun 2021, 12:45 PM
Ali_combination
Ali_combination - avatar
- 2
Всем привет
5th Jun 2021, 5:21 PM
makster
makster - avatar