python类之间变量的传输的时候变量是字符串和变量是列表或者字典是完全不同的:
试着把self.time的格式分别用字符串,列表,和字典,看输出的结果:
import time class Book: def __init__(self): self.time=[None] def update_time(self): self.time[0]=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) class Strategy: def __init__(self,book): self.time=book.time book=Book() strategy=Strategy(book) for i in range(10): time.sleep(1) book.update_time() print( strategy.time)
self.time是字符串的形式:
None
None
None
None
None
None
None
None
None
None
self.time是列表的形式:
['2018-11-21 19:49:04']
['2018-11-21 19:49:05']
['2018-11-21 19:49:06']
['2018-11-21 19:49:07']
['2018-11-21 19:49:08']
['2018-11-21 19:49:09']
['2018-11-21 19:49:10']
['2018-11-21 19:49:11']
['2018-11-21 19:49:12']
['2018-11-21 19:49:13']
self.time是字典形式的:
{'datetime': '2018-11-21 19:51:40'}
{'datetime': '2018-11-21 19:51:41'}
{'datetime': '2018-11-21 19:51:42'}
{'datetime': '2018-11-21 19:51:43'}
{'datetime': '2018-11-21 19:51:44'}
{'datetime': '2018-11-21 19:51:45'}
{'datetime': '2018-11-21 19:51:46'}
{'datetime': '2018-11-21 19:51:47'}
{'datetime': '2018-11-21 19:51:48'}
{'datetime': '2018-11-21 19:51:49'}