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
21 Respuestas
+ 5
before taking a decision, i would like to see your code please. place the code in playground and link it here.
thanks!
+ 4
make them global.
+ 2
Use global ,for eg you have local veriable called "list1" to make it global use "global list1" inside func. Simple🙂👍
+ 2
😊😊👍
actually it was Akshay code I just edited that code bite for showing simple way
+ 2
global
+ 2
You can return variable, make them static to global
+ 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.
+ 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 👍
+ 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 ..
+ 1
def f():
global a
a = "Hello" #Simple Variable
+ 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)
+ 1
Solving recursion using an abstract and generic algorithm to any problem. (Defs.: Software pattern)
https://code.sololearn.com/ct0FMgR963HC/?ref=app
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
0
Yash Wable 🇮🇳 got it ! Thanks !❤🙌
0
You can try this method also
https://code.sololearn.com/cTAFrlxARfUI/?ref=app
0
Yash Wable 🇮🇳 Yep , thats a good way too .. thank you 😊👍
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 ...
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 .
0
David Ordás thanks for your precious reply !
- 2
Всем привет