0

I'm trying to set a local variable in main() from insideFCT() which is starts in main():

def insideFCT():
     print "inside"
     info = datetime.datetime.now()
     print info

def main():
     print "starting main"
     insideFCT()
     functionThatPrint(info) #for the example functionThatPrint() acts like print()

main()

I need to find a way so that it gives me:

starting main
inside
2013-09-19...
2013-09-19...
  • I can't modify main() at all (it means I can't add "info = insideFCT()" and add a return to insideFCT())
  • I can't use global variables because the function is going to be use several times at the same time (threads)
  • Of course this is not at all about getting the time, datetime.datetime.now() represents an input I can't control

My idea is to find something specific to each main() when they start, store "info" in a global dictionnary as globDict[TheThingSpecific]=info and then I can acces it using globDict[TheThingSpecific] in functionThatPrint() (Indeed, functionThatPrint() is called in the same function as insideFCT() so they have the same "specific thing")

I just found the "something specific" I was looking for. It's threading.current_thread()

SOLUTION:

  • insideFCT() stores info into globalDict[str(threading.current_thread())]
  • then I can access it in functionThatPrint() using the same line because it's the same thread : globalDict[str(threading.current_thread())]
martineau
  • 119,623
  • 25
  • 170
  • 301
elbajo
  • 683
  • 2
  • 8
  • 17
  • 2
    Why do you have these restrictions? – alecxe Sep 20 '13 at 09:29
  • Why can't you pass info to the main method? – Rami Sep 20 '13 at 09:36
  • @alecxe it's part of my project, the first restriction is because main is going to be created by other people and i want it to be as simple as possible (even if "insideFCT" is a real mess). And the other ones I think I already explained it. – elbajo Sep 20 '13 at 09:37
  • @Rami Helmy as I said I can't modify main() (I can add a return to insideFCT() on it's own it's useless) – elbajo Sep 20 '13 at 09:39
  • the variable "info" is a local variable inside the function insideFCT. To be able to access it from the second function "main", you should either declare it as global or pass it as a parameter to the second function. Unless there is something like a Friend function concept in Python like the Friend class in C++ – Rami Sep 20 '13 at 09:40
  • Yes, I guess the last thing is what i'm looking for. – elbajo Sep 20 '13 at 09:45
  • And just for my knowledge, is the opposite as difficult? (get a variable form main to functionThatPrint() without putting it in arguments?) – elbajo Sep 20 '13 at 09:47
  • as a second thought, why don't you use the func_dict of the first function to store whatever variables you want, and then access it from the second function? – Rami Sep 20 '13 at 09:47
  • I just tried: it's a good idea but func_dict is the same for all my threads (i start them on the same python file) so it's pretty much the same as using global variables – elbajo Sep 20 '13 at 09:59
  • An other way would be to find something specific to each function when it's starts so that i can store it in a global dictionnary and then get it in functionThatPrint. – elbajo Sep 20 '13 at 10:10
  • @RamiHelmy `friend` is about access control, this one is about scope. the scope in python is lexical; there is no way to do it in python. – Elazar Sep 20 '13 at 10:42

1 Answers1

0

There is no way to set a local variable inside a function scope in python from outside. The scope to which the name info belongs is decided at "compile time" - that is, when the interpreter executes the def main() command.

Since the is no binding to info inside main(), it is assumed to be global or built-in. That's it.

Elazar
  • 20,415
  • 4
  • 46
  • 67
  • May be but there are ways around it like my solution (even if it's not the cleanest thing ever) – elbajo Sep 20 '13 at 13:29
  • So what you want is basically thrad-local storage? – Elazar Sep 20 '13 at 14:46
  • Yes I didn't see it like that but you're right all I need is a way to make my thread communicate and have shared variables. With that it mind I look around and I think "Queue" is a good solution isn't it? – elbajo Sep 20 '13 at 15:10
  • @user2707890 perhaps [this answer](http://stackoverflow.com/questions/104983/what-is-thread-local-storage-in-python-and-why-do-i-need-it) may help. – Elazar Sep 20 '13 at 15:17