摘要: ``` #coding=utf-8 #内部类 class MyClass(object): class InnerClass: pass # 正则表达式 # 通过re模块来访问 import re m=re.search(r'foo','seafood') print m m.group() print m m=re.search(r'bar','seafood') print m ... 阅读全文
posted @ 2016-01-04 23:21 yufenghou 阅读(100) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 #python还支持动态的实力属性,即那些没有在类定义里生命的属性, #可以“凭空”创造出来 john.tatto='Mom' #继承 class EmployeeAddressBookEntry(AddressBookEntry): def __init__(self,name,phone,id,social): AddressBookEntry... 阅读全文
posted @ 2016-01-04 23:15 yufenghou 阅读(99) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 class AddressBookEntry(object): version=0.1 def __init__(self, name,phone): self.name = name self.phone= phone def update_phone(self,phone): self.phone= phone ''' version属于... 阅读全文
posted @ 2016-01-04 23:08 yufenghou 阅读(127) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 @doco def foo(): pass ''' deco把foo函数拿过来,加上一些额外的功能再重新赋值给foo,如下 ''' foo=deco(foo) def log(func): def wrappedFunc(): print "*** %s() called" % func.__name__ return func() ret... 阅读全文
posted @ 2016-01-04 22:53 yufenghou 阅读(108) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 # 函数 def foo(x): print x foo(123) # import httplib def check_web_server(host,port,path): h=httplib.HTTPConnection(host,port) h.request('GET',path) resp=h.getresponse() prin... 阅读全文
posted @ 2016-01-04 21:04 yufenghou 阅读(131) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 # 函数 def foo(x): print x foo(123) # import httplib def check_web_server(host,port,path): h=httplib.HTTPConnection(host,port) h.request('GET',path) resp=h.getresponse() prin... 阅读全文
posted @ 2016-01-04 21:02 yufenghou 阅读(93) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 f=open('text.txt','w') f.write('foo\n') f.write('bar\n') f.close() f=open('test.txt','r') for line in f: print line.rstrip() ``` 阅读全文
posted @ 2016-01-04 20:54 yufenghou 阅读(102) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 try: get_mutex() do_some_stuff() except (IndexError,KeyError,AttributeError),e: log("ERROR:data retrieval accessing a non-existent element") finally: free_mutex() # 用raise抛出异常... 阅读全文
posted @ 2016-01-04 20:51 yufenghou 阅读(137) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 ''' 如果运行时发生异常的话,解释器会查找相应的处理函数。要是在当前函数里没有 找到的话,它会将异常传递给上层的调用函数,看看那里能不能处理。如果在在最 外层还没有找到的话,解释器就会推出,同时打印出traceback以便让用户找出 错误产生的原因。 ''' try: f=open(filename,'r') except IOError,e: retu... 阅读全文
posted @ 2016-01-04 20:27 yufenghou 阅读(105) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 #enumerate是一个内置函数 data=(123,'abc',3.14) for i,value in enumerate(data): print i,value ``` 阅读全文
posted @ 2016-01-04 20:17 yufenghou 阅读(129) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 #条件转化 data=raw_input("enter 'y' or 'n'") if data[0]=='y': print "you typed y." elif data[0]=='n': print "you typed n." else: print 'invalid key entered!' # 循环 i=0 while i<5: ... 阅读全文
posted @ 2016-01-04 19:50 yufenghou 阅读(95) 评论(0) 推荐(0)
摘要: ``` #coding=utf-8 d={'title':'python web development','year':2008} print d.setdefault('pub','addision wesley') del d['pub'] print len(d) ``` 阅读全文
posted @ 2016-01-04 19:43 yufenghou 阅读(108) 评论(0) 推荐(0)