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: ...
如果没有异常,则执行else语句,如果没有对应的异常类型则会向上层抛出异常# 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))#调用的是new_F函数print(square_sum(4,3))
装饰器即装饰器类示例
decorator
print("I'm %(name)s. I'm %(age)d year old" % {'name':'Vamei', 'age':99})(使用字典传递真实值)
常用内置函数:
class C(B): def method(self, arg): # This does the same thing as: # super(C, self).method(arg),相当于B.method(self,arg),但在多继承情况下super函数能避免相同基类被多次调用 super().method(arg)
map(函数对象,list...):功能是将函数对象依次作用于list的每一个元素,每次作用的结果存储在返回的循环对象中,如果函数对象有多个参数,则后面可以有多个list,map函数每次从所有的list中取出一个值,作为函数的参数
filter(函数对象,list...):功能是将函数对象作用于多个元素,如果函数对象返回的是True,则返回该次的元素存储在循环对象中
reduce(函数对象,list):函数对象只能接受两个参数,可以累进的从list中取值,每一次调用函数对象的返回值与list中后面一个元素作为下次函数对象调用的参数。3.x中需要引入functools包
内置函数列表:
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()
新闻热点
疑难解答