def pow(x, n = 2):
r = 1
while n > 0:
r *= x
n -= 1
return r
print(pow(5)) # output: 25
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
def bad_append(new_item, a_list=[]):
a_list.append(new_item)
return a_list
print(bad_append('1'))
print(bad_append('2'))
['1'] ['2']
['1'] ['1', '2']
import datetime as dt
from time import sleep
def log_time(msg, time=dt.datetime.now()):
sleep(1) # 线程暂停一秒
print("%s: %s" % (time.isoformat(), msg))
log_time('msg 1')
log_time('msg 2')
log_time('msg 3')
2017-05-17T12:23:46.327258: msg 1 2017-05-17T12:23:46.327258: msg 2 2017-05-17T12:23:46.327258: msg 3
def bad_append(new_item, a_list=[]):
print('address of a_list:', id(a_list))
a_list.append(new_item)
return a_list
print(bad_append('1'))
print(bad_append('2'))
address of a_list: 31128072 ['1'] address of a_list: 31128072 ['1', '2']
def immutable_test(i = 1):
print('before operation, address of i', id(i))
i += 1
print('after operation, address of i', id(i))
return i
print(immutable_test())
print(immutable_test())
before operation, address of i 1470514832 after operation, address of i 1470514848 2 before operation, address of i 1470514832 after operation, address of i 1470514848 2
def good_append(new_item, a_list = None):
if a_list is None:
a_list = []
a_list.append(new_item)
return a_list
print(good_append('1'))
print(good_append('2'))
print(good_append('c', ['a', 'b']))
import datetime as dt
from time import sleep
def log_time(msg, time = None):
if time is None:
time = dt.datetime.now()
sleep(1)
print("%s: %s" % (time.isoformat(), msg))
log_time('msg 1')
log_time('msg 2')
log_time('msg 3')
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有