Python | How to make local variable global, after script execution -


Here's the code I need to do i getting a way to create a global so that Again, the value of i will be incremented by 1 rather than 1. I have the trace function to work in code

in the second script, which I have embedded in 'main' I do All this is being done from Java. <__future__ import nested_scopes import sys import time from start time = time.time () timeLimit = 5000 DRR traceat (frame, event, RGR): if event == "line": elapsedTime = ( (Time.time () - startTime) * 1000) Time elapsed & gt; Time limit: Increase the exception, "execution time" + str (timelitit) + "has exceeded the time limit of milliseconds. The script will now end" return trait sys.settrace (traceit) def main ______ (): try: I + Exclude 1 = NameError: I = 1 main ______ ()

It is unfortunate that You have edited this question very heavy that people's answers appear unnecessary.

There are several ways to create a variable scout within such a function, whose value remains unchanged from call to call. They all take advantage of the fact that the functions are first-class objects, which means that they can have properties. For example:

  def f (x): if not a heater (f, "i"): setattr (f, "i", 0) fi + = x return return  

There is also a hack for using a list as a default value for the argument, and then when calling a function, no value is provided for the argument:

  def f (X, my_list = [0]): my_list [0] = my_list [0] + x my_list back [0]  

... But I would not recommend using it unless you understand why it works, and maybe even this Do not be.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -