Friday 15 May 2015

return - Python: passing values from function to function -


I am fighting with it for three hours.

ETA- It should have been mentioned, but for the purposes of this class, global variables have not been allowed to be used.

In the main () function, I want to run a function first if only if the whole function is running for the first time. The first loop function starts some variables and prints some information which is not interesting if it is not the first time it is viewed.

I duly:

  # Before we can do any work here, start the necessary variable to run count = 0 def firstPass (): x = 5 y = 2 print "Count some things" count = count + 1 print "calculate in the first", count count on count, x, y df main (): print "calculate in main", count = count = 0 0: firstPass () else: # A complete set of other goods that are not going to run on # 1 #, because other stuff is user-modified X and Y.V. Using the Q, and resetting the start value to # must defeat itself Main pass ()   

This returns correctly on the first pass, but at the later time Returns:

  In the main calculation 1   

This is also a problem within my other user-modified x and y values ​​for other functions. Although I have not modified them here, I included them because I need to pass several values ​​between the functions in the code later, but who wants to read all, when I could put them here for example. .

I was under the assumption that

  back [variable]     

You have to:

  def firstPass (): global To get the variable to update   

count

You ask the keyword Global to load and store the interpreter in the global count variable instead of saving the interpreter in the local variable of that name. For example:

I global :

  & gt; & Gt; & Gt; I define two functions without using; A = 0> gt; & Gt; & Gt; Def foo (): ... a + = 1 ... back to a & gt; & Gt; & Gt; Def Bar (): ... Global A ... a + = 1 ... After separating both tasks by using a module, it becomes clear that What's the difference:  
  & gt; & Gt; & Gt; Import vs & gt; & Gt; & Gt; Dis.dis (foo) 2 0 LOAD_FAST 0 (A) 3 LOAD_CONST 1 (1) 6 INPLACE_ADD 7 STORE_FAST 0 (A) 3 10 LOAD_FAST 0 (A) 13 Returns_VALUE & gt; & Gt; & Gt; Dis.dis 3 0 LOAD_GLOBAL 0 (A) 3 LOAD_CONST 1 (1) 6 INPLACE_ADD 7 STORE_GLOBAL 0 (A) 4 10 LOAD_GLOBAL 0 (A) 13 return_ VALUE    

No comments:

Post a Comment