0

I have the following code, but I'm not quite sure how it's going to work.

I'll execute the following:

date = strftime("%Y-%m-%d %H:%M:%S")

and it will return exact date and time of the moment I call this. I want to use the exact output of this date and time in another function which is going to be execute 2-3 seconds later.

How can I pass the date variable here and not use the new current time and date?

Aurora0001
  • 13,139
  • 5
  • 50
  • 53
Jecki
  • 802
  • 3
  • 16
  • 32

1 Answers1

1

Does anything stop you from passing date as a parameter to your other function ?

http://www.tutorialspoint.com/python/python_functions.htm

If you can't pass it as a parameter, and both functions are within the same object / class, you can use self.date = strftime("%Y-%m-%d %H:%M:%S") and on your other function use self.date when you need to read it.

https://docs.python.org/3/tutorial/classes.html#class-objects

Finally you can simply set a global variable, but depending on your project, this may cause bugs, if for example the code is multi-threaded (like a web application).

Using global variables in a function other than the one that created them

Community
  • 1
  • 1
Giannis
  • 5,286
  • 15
  • 58
  • 113
  • i've test code as following : def ready(): readyDate = strftime("%Y-%m-%d %H:%M:%S") print readyDate return; – Jecki Jul 31 '16 at 12:25
  • when ever i call this , it will give me the current date and time for sure , my code include differents functions one of these function will use def ready and get output o it which is should be 2-3 sec behind .. – Jecki Jul 31 '16 at 12:27