0

The variable created by time.time() shouldn't be immutable, right? Therefore, I should be able to change it to something else. I'm having trouble doing this, and it doesn't seem like the .sleep() method helps.

I set a variable e, to the current time. I print e. Then I update the variable e, using a function. Then I print e. The two printed values should be different, but they aren't. How do I achieve the correct update to the "global" variable e?

import time

e = time.time()
print ('e is %f') %e

time.sleep(1.12)
def uu(x):
    #time.sleep(2)
    x = time.time()

uu(e)
print ('%f') %e

No matter when I put the time delay, the two prints of e are exactly the same. However, I passed e to the function, and e is not immutable, but it doesn't change with the new assignment statement (even if there was a time delay before the call of the function [externally or internally]).

I'm expecting an output like

e is 1432940101.000643
1432940102.120643

Where the first value a second value differ by any amount.

VISQL
  • 1,960
  • 5
  • 29
  • 41
  • `e` is a name. It refers to a `float` object returned by `time.time()` function. Your code wouldn't change `e` even if floats weren't immutable in Python. Look at the pictures [Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) and see [Facts and Myths about Python names and values](http://pyvideo.org/video/3466/facts-and-myths-about-python-names-and-values). If you read russian, see [this answer](http://ru.stackoverflow.com/a/419211/23044). Update your question if something is unclear. – jfs May 30 '15 at 16:56
  • related: [In Python, why can a function modify some arguments as perceived by the caller, but not others?](http://stackoverflow.com/q/575196/4279) – jfs May 30 '15 at 16:59
  • @J.F.Sebastian : From your comment (before looking at the links) it seems you're saying that "uu(e)" is calling that function on a float, not a variable that holds a float, and therefore this doesn't work. – VISQL Jun 05 '15 at 18:58

1 Answers1

0

This is a scope/namespace issue. e is declared Globally. Even though I pass e into the function uu, that doesn't change the globally defined value of e.

However, if I were to put a print statement within uu then e with the scope of that function, would have a different value.

VISQL
  • 1,960
  • 5
  • 29
  • 41