In:frompathlibimportPathIn:Path.home()Out:PosixPath('/Users/dongweiming')# 用户目录In:path=Path('/user')In:path/'local'# 非常直观Out:PosixPath('/user/local')In:str(path/'local'/'bin')Out:'/user/local/bin'In:f=Path('example.txt')In:f.write_bytes('This is the content'.encode('utf-8'))Out[16]:19In:withf.open('r',encoding='utf-8')ashandle:# open现在是方法了....:print('read from open(): {!r}'.format(handle.read()))....:readfromopen():'This is the content'In:p=Path('touched')In:p.exists()# 集成了多个常用方法Out:FalseIn:p.touch()In:p.exists()Out:TrueIn:p.with_suffix('.jpg')Out:PosixPath('touched.jpg')In:p.is_dir()Out:FalseIn:p.joinpath('a','b')Out:PosixPath('touched/a/b')
In:name='Fred'In:f'My name is {name}'Out:'My name is Fred'In:fromdatetimeimport*In:date=datetime.now().date()In:f'{date} was on a {date:%A}'Out:'2018-01-17 was on a Wednesday'In:deffoo():....:return20....:In:f'result={foo()}'Out:'result=20'
@magic_arguments.magic_arguments()# 最上面@magic_arguments.argument('--breakpoint','-b',metavar='FILE:LINE',help=""" Set break point at LINE in FILE. """)# 这种argument可以有多个@magic_arguments.argument('statement',nargs='*',help=""" Code to run in debugger. You can omit this in cell magic mode. """)@line_cell_magicdefdebug(self,line='',cell=None):args=magic_arguments.parse_argstring(self.debug,line)# 要保持第一个参数等于这个方法名字,这里就是self.debug...
classCounter(dict):...def__missing__(self,key):'The count of elements not in the Counter is zero.'# Needed so that self[missing_item] does not raise KeyErrorreturn0
In[37]:a=1In[38]:b=1In[39]:print('a is b',bool(aisb))('a is b',True)In[40]:c=999In[41]:d=999In[42]:print('c is d',bool(cisd))('c is d',False)# 原因是python的内存管理,缓存了-5 - 256的对象In[43]:print('256 is 257-1',256is257-1)('256 is 257-1',True)In[44]:print('257 is 258-1',257is258-1)('257 is 258-1',False)In[45]:print('-5 is -6+1',-5is-6+1)('-5 is -6+1',True)In[46]:print('-7 is -6-1',-7is-6-1)('-7 is -6-1',False)In[47]:a='hello world!'In[48]:b='hello world!'In[49]:print('a is b,',aisb)('a is b,',False)# 很明显 他们没有被缓存,这是2个字段串的对象In[50]:print('a == b,',a==b)('a == b,',True)# 但他们的值相同# But, 有个特例In[51]:a=float('nan')In[52]:print('a is a,',aisa)('a is a,',True)In[53]:print('a == a,',a==a)('a == a,',False)# 亮瞎我眼睛了~