?????????????????
??????????python??????package???????????????????е?????????class???????import????__init__.py?????__all__???????list????д????import?????????????? ???????????import??????? ?????import??????????????

????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????from base import APIBase
????from client import Client
????from decorator import interface?? export?? stream
????from server import Server
????from storage import Storage
????from util import (LogFormatter?? disable_logging_to_stderr??
????enable_logging_to_kids?? info)
????__all__ = ['APIBase'?? 'Client'?? 'LogFormatter'?? 'Server'??
????'Storage'?? 'disable_logging_to_stderr'?? 'enable_logging_to_kids'??
????'export'?? 'info'?? 'interface'?? 'stream']
????with?????
????with??????????????????Э?????? ?????????Э?????__enter__??__exit__?????????? with??佨???????????????????????????????н?????????????
??????????????????????with????????? ????????????????????????
????# ????with??ó???
????with open("test.txt"?? "r") as my_file:  # ??? ??__enter__()?????????????????my_file??
????for line in my_file:
????print line
????????????????????£? ?? Python ?? with ???
???????????????????????????????????????Э????????????__enter__??__exit__??????
????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????class MyWith(object):
????def __init__(self):
????print "__init__ method"
????def __enter__(self):
????print "__enter__ method"
????return self  # ????????as??????
????def __exit__(self?? exc_type?? exc_value?? exc_traceback):
????print "__exit__ method"
????if exc_traceback is None:
????print "Exited without Exception"
????return True
????else:
????print "Exited with Exception"
????return False
????def test_with():
????with MyWith() as my_with:
????print "running my_with"
????print "------?????-----"
????with MyWith() as my_with:
????print "running before Exception"
????raise Exception
????print "running after Exception"
????if __name__ == '__main__':
????test_with()
??????н?????£?
????__init__ method
????__enter__ method
????running my_with
????__exit__ method
????Exited without Exception
????------?????-----
????__init__ method
????__enter__ method
????running before Exception
????__exit__ method
????Exited with Exception
????Traceback (most recent call last):
????File "bin/python"?? line 34?? in <module>
????exec(compile(__file__f.read()?? __file__?? "exec"))
????File "test_with.py"?? line 33?? in <module>
????test_with()
????File "test_with.py"?? line 28?? in test_with
????raise Exception
????Exception
???????????????__enter__?????? ??????with???????? ?????__exit__?????????? ????? ???????????????????
????filter???÷?
???????filter????? map??reduce??????????Щ?? filter??????????? ???????????????Щ????
????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????lst = [1?? 2?? 3?? 4?? 5?? 6]
????# ??????????????True?? ???????False???????
????print filter(lambda x: x % 2 != 0?? lst)
????#??????
????[1?? 3?? 5]
??????????ж?
????????????????? ?????????????????? ??????else?????
????lst = [1?? 2?? 3]
????new_lst = lst[0] if lst is not None else None
????print new_lst
????# ??????
????1
??????????????
???????????????????????
????# ?????????
????def singleton(cls):
????instances = dict()  # ??????
????def _singleton(*args?? **kwargs):
????if cls not in instances:  #?????????? ?????????????
????instances[cls] = cls(*args?? **kwargs)
????return instances[cls]
????return _singleton
????@singleton
????class Test(object):
????pass
????if __name__ == '__main__':
????t1 = Test()
????t2 = Test()
????# ??????????????
????print t1?? t2
????staticmethod?????
??????????????????Σ? ????????????????
???????????????? ???е????????????????
????classmethod??????? ????(????о??????????OC?е?????)?? ???е??????????????
????staticmethod??????? ????κ????????. python?е?????????????C++?е???????
????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????class A(object):
????# ??????????
????def foo(self?? x):
????print "executing foo(%s?? %s)" % (self?? x)
????@classmethod   # ???classmethod???????
????def class_foo(cls?? x):
????print "executing class_foo(%s?? %s)" % (cls?? x)
????@staticmethod  # ???staticmethod???????
????def static_foo(x):
????print "executing static_foo(%s)" % x
????def test_three_method():
????obj = A()
????# ?????????????????
????obj.foo("para")  # ???obj????????????????????????? ??self
????obj.class_foo("para")  # ????????????????????? ??cls
????A.class_foo("para")  #??????????????
????obj.static_foo("para")  # ?????????????κ?????????? ???????????????????е???
????A.static_foo("para")
????if __name__ == '__main__':
????test_three_method()
????# ???????
????executing foo(<__main__.A object at 0x100ba4e10>?? para)
????executing class_foo(<class '__main__.A'>?? para)
????executing class_foo(<class '__main__.A'>?? para)
????executing static_foo(para)
????executing static_foo(para)
????property?????
?????????????????
??????property???????????????????л?(????????????get??set????)??
????#python???????
????property(fget=None?? fset=None?? fdel=None?? doc=None)
????fget?????????????????fset??????????????????fdel???????????????doc??????????(????????)???????????????Щ?????????????
????property??????????getter()?? setter()??delete() ?????fget?? fset??fdel?? ???????????У?
????class Student(object):
????@property  #????property.getter(score) ????property(score)
????def score(self):
????return self._score
????@score.setter #????score = property.setter(score)
????def score(self?? value):
????if not isinstance(value?? int):
????raise ValueError('score must be an integer!')
????if value < 0 or value > 100:
????raise ValueError('score must between 0 ~ 100!')
????self._score = value
????iter???
???????yield??__iter__????????????????????????????
???????__str__????д?? ?????????????????????????
????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????class TestIter(object):
????def __init__(self):
????self.lst = [1?? 2?? 3?? 4?? 5]
????def read(self):
????for ele in xrange(len(self.lst)):
????yield ele
????def __iter__(self):
????return self.read()
????def __str__(self):
????return '??'.join(map(str?? self.lst))
????__repr__ = __str__
????def test_iter():
????obj = TestIter()
????for num in obj:
????print num
????print obj
????if __name__ == '__main__':
????test_iter()
????????partial
????partial????????C++?зo???(????????)??
??????stackoverflow????????????partial?????з????
????def partial(func?? *part_args):
????def wrapper(*extra_args):
????args = list(part_args)
????args.extend(extra_args)
????return func(*args)
????return wrapper
????????????????????????Щ???????????????????????????? ??????????????У?
????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????from functools import partial
????def sum(a?? b):
????return a + b
????def test_partial():
????fun = partial(sum?? 2)   # ?????????????? fun??????????????????????????
????print fun(3)  # ?????е????sum(2?? 3)
????if __name__ == '__main__':
????test_partial()
????# ??н??
????5