dict example
set example
def func(a,b,c): print(a,b,c)args = (1,3,4)func(*args)
實例方法
__class__ __bases__ example
descriptor example
try: ...
except exception1: ...
except exception2: ...
except: ...
else: ...
finally: ...如果沒有異常,則執(zhí)行else語句,如果沒有對應(yīng)的異常類型則會向上層拋出異常# with context manager with open("new.txt", "w") as f: print(f.closed) f.write("Hello World!") print(f.closed)
property example
property example
__getattr__ example
def decor(F): def new_F(a,b): print('decor') return F(a,b) return new_F@decordef square_sum(a, b): return a**2 + b**2print(square_sum(2,3))#調(diào)用的是new_F函數(shù)print(square_sum(4,3))
裝飾器即裝飾器類示例decorator
print("I'm %(name)s. I'm %(age)d year old" % {'name':'Vamei', 'age':99})(使用字典傳遞真實值)常用內(nèi)置函數(shù):class C(B): def method(self, arg): # This does the same thing as: # super(C, self).method(arg),相當(dāng)于B.method(self,arg),但在多繼承情況下super函數(shù)能避免相同基類被多次調(diào)用 super().method(arg)
map(函數(shù)對象,list...):功能是將函數(shù)對象依次作用于list的每一個元素,每次作用的結(jié)果存儲在返回的循環(huán)對象中,如果函數(shù)對象有多個參數(shù),則后面可以有多個list,map函數(shù)每次從所有的list中取出一個值,作為函數(shù)的參數(shù)
filter(函數(shù)對象,list...):功能是將函數(shù)對象作用于多個元素,如果函數(shù)對象返回的是True,則返回該次的元素存儲在循環(huán)對象中
reduce(函數(shù)對象,list):函數(shù)對象只能接受兩個參數(shù),可以累進的從list中取值,每一次調(diào)用函數(shù)對象的返回值與list中后面一個元素作為下次函數(shù)對象調(diào)用的參數(shù)。3.x中需要引入functools包
內(nèi)置函數(shù)列表:
Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod() bin() eval() int() open() str() bool() exec() isinstance() ord() sum() bytearray() filter() issubclass() pow() super() bytes() float() iter() print() tuple() callable() format() len() property() type() chr() frozenset() list() range() vars() classmethod() getattr() locals() repr() zip() compile() globals() map() reversed() __import__() complex() hasattr() max() round() delattr() hash() memoryview() set()
新聞熱點
疑難解答