看了很多关于reload的文章,竟然没有一篇有基础的使用代码,对新人极度不友好,搞得我弄了好久,自己写一篇,方便自己
先给代码:
首先写一个HotUpdateTarget.py
class HotUpdateTarget(object):
def __init__(self):
super(HotUpdateTarget, self).__init__()
self.num = 0
def logic(self,n):
n = n+1# 本例子主要修改这里
return n
然后写一个test.py运行HotUpdateTarget类
from time import sleep
import HotUpdateTarget
test1 = HotUpdateTarget
test = test1.HotUpdateTarget()
n = 0
while True:
sleep(1)
n = test.logic(n)
if n > 15:
reload(HotUpdateTarget)
test = test1.HotUpdateTarget()
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print n
然后直接修改HotUpdateTarget.py,并保存
class HotUpdateTarget(object):
def __init__(self):
super(HotUpdateTarget, self).__init__()
self.num = 0
def logic(self,n):
n = n*2# 修改后
return n
打印出来的结果为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>>>>>>>>>>>>>>>>>>>>>>>>>>
16
>>>>>>>>>>>>>>>>>>>>>>>>>>>
32
>>>>>>>>>>>>>>>>>>>>>>>>>>>
64
>>>>>>>>>>>>>>>>>>>>>>>>>>>
128
>>>>>>>>>>>>>>>>>>>>>>>>>>>
256
>>>>>>>>>>>>>>>>>>>>>>>>>>>
512
>>>>>>>>>>>>>>>>>>>>>>>>>>>
1024
>>>>>>>>>>>>>>>>>>>>>>>>>>>
2048
>>>>>>>>>>>>>>>>>>>>>>>>>>>
4096
>>>>>>>>>>>>>>>>>>>>>>>>>>>
8192
>>>>>>>>>>>>>>>>>>>>>>>>>>>
16384
>>>>>>>>>>>>>>>>>>>>>>>>>>>
32768
>>>>>>>>>>>>>>>>>>>>>>>>>>>
65536
>>>>>>>>>>>>>>>>>>>>>>>>>>>
131072
>>>>>>>>>>>>>>>>>>>>>>>>>>>
262144
>>>>>>>>>>>>>>>>>>>>>>>>>>>
524288
>>>>>>>>>>>>>>>>>>>>>>>>>>>
1048576
>>>>>>>>>>>>>>>>>>>>>>>>>>>
2097152
>>>>>>>>>>>>>>>>>>>>>>>>>>>
4194304
>>>>>>>>>>>>>>>>>>>>>>>>>>>
8388608
>>>>>>>>>>>>>>>>>>>>>>>>>>>
16777216
>>>>>>>>>>>>>>>>>>>>>>>>>>>
33554432
>>>>>>>>>>>>>>>>>>>>>>>>>>>
67108864
>>>>>>>>>>>>>>>>>>>>>>>>>>>
134217728
>>>>>>>>>>>>>>>>>>>>>>>>>>>
268435456
>>>>>>>>>>>>>>>>>>>>>>>>>>>
536870912
>>>>>>>>>>>>>>>>>>>>>>>>>>>
1073741824
>>>>>>>>>>>>>>>>>>>>>>>>>>>
2147483648
>>>>>>>>>>>>>>>>>>>>>>>>>>>
4294967296
>>>>>>>>>>>>>>>>>>>>>>>>>>>
8589934592
>>>>>>>>>>>>>>>>>>>>>>>>>>>
17179869184
需要注意的是,在reload之后,需要重新生成一个新的HotUpdateTarget对象,才会生效。旧的HotUpdateTarget类会依旧执行原来的逻辑…
PS:使用的编辑器为pychram,Python版本2.7