abs() Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned. 返回一个数的绝对值。参数可以是整数或浮点数.。如果参数是复数,则返回它的数量级.
all(iterable)Return True if all elements of the iterable are true (or if the iterable is empty).返回bool,集合中所有元素不为0、''、False时返回True,若集合为空,返回True
any(iterable)Return True if any element of the iterable is true. If the iterable is empty, return False返回bool,当集合有任意元素为True时,返回True。集合为空或元素均为0/''/False时,返回False。
ascii() As , return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by using \x, \u or \U escapes.
bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.将整数x转换为二进制字符串,结果返回一个有效的Python表达式,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer
isinstance(object, classinfo)Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.返回bool,验证参数一是否为参数二中的类型。参数二可以为元组,参数一为元组中所属类型,返回true。
dict()class dict(**kwarg)class dict(mapping, **kwarg)class dict(iterable, **kwarg)Create a new dictionary. The dict object is the dictionary classdict对象用来,创建字典类
map(function, iterable, ...)Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.
dir([object])Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.If the object has a method named __dir__(), this method will be called and must return the list of attributes. This allows objects that implement a custom __getattr__() or __getattribute__() function to customize the way dir() reports their attributes.If the object does not provide __dir__(), the function tries its best to gather information from the object’s __dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattr__().
hasattr(object, name)The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)判断该属性是否在对象中
setattr(object, name, value)This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.为对象的属性设置值
getattr(object, name[, default])Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.获取对象属性,如果对象不存在可以设置返回默认值getattr(obj, 'attribute',default)
class type(object)class type(name, bases, dict)With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and is copied to a standard dictionary to become the __dict__ attribute. For example, the following two statements create identical type objects:>>> class X:... a = 1...>>> X = type('X', (object,), dict(a=1)) 只有一个参数时,返回一个新的对象类型object.__class__(即判断后的对象类型) 有三个参数时,返回一个新的对象类型,可以作为动态的创建类,X = type('X', (object,), dict(a=1))与一般定义类的方法一致。第一个参数为类X的__name__属性的值,其中第二个参数为父类,当有多个父类时需注意顺序(父类3,父类2,父类1,object),第三个参数为类属性。 同时,也可以将方法绑定到类上: def Y(self,name='Bean'): print('My name is',name)
H = type('hello',(object,),dict(hello=y)) >>>h = H() >>>h.hello() >>>print(H.__name__) My name is Bean hello
class property(fget=None, fset=None, fdel=None, doc=None)# Return a property attribute.'返回property属性' #fget is a function for getting an attribute value. '属性值的get方法名称' # fset is a function for setting an attribute value. '属性值的set方法名称' # fdel is a function for deleting an attribute value. '属性值del的方法名称' # And doc creates a docstring for the attribute. '属性值的doc文档内容' # A typical use is to define a managed attribute x: //使用方法定义属性及属性值的操作方法
class C: def __init__(self): self._x = None def getx(self): return self._x def setx(self,value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, 'Test Property') c = C() c.x = 123 print(c.x) print(C.x.__doc__)
# If c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter. # If given, doc will be the docstring of the property attribute. # Otherwise, the property will copy fget‘s docstring (if it exists). # This makes it possible to create read-only properties easily using property() as a decorator:使用装饰器模式设置属性值只读
class Parrot: def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage
p = Parrot() print(p.voltage) # 使用装饰器声明和操作属性 # The @property decorator turns the voltage() method into a “getter” for a read-only attribute with the same name, # and it sets the docstring for voltage to “Get the current voltage.” # A property object has getter, setter, and deleter methods usable as decorators that create a copy of # the property with the corresponding accessor function set to the decorated function. # This is best explained with an example:
class CC: def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x cc = CC() cc.x = 123 print(cc.x) print(CC.x.__doc__)
class CC 与Class C两种方式均可,最好使用class CC的方式
callable(object)Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a __call__() method.判断一个变量是对象还是函数呢。判断一个对象是否能被调用,能被调用的对象就是一个Callable对象。当能被调用时返回True,否则返回False
super([type[, object-or-type]])Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.The __mro__ attribute of the type lists the method resolution search order used by both getattr() and super(). The attribute is dynamic and can change whenever the inheritance hierarchy is updated.If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods).There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).For both use cases, a typical superclass call looks like this:class C(B): def method(self, arg): super().method(arg) # This does the same thing as: # super(C, self).method(arg) Note that super() is implemented as part of the binding process for explicit dotted attribute lookups such as super().__getitem__(name). It does so by implementing its own __getattribute__() method for searching classes in a predictable order that supports cooperative multiple inheritance. Accordingly, super() is undefined for implicit lookups using statements or operators such as super()[name].Also note that, aside from the zero argument form, super() is not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references. The zero argument form only works inside a class definition, as the compiler fills in the necessary details to correctly retrieve the class being defined, as well as accessing the current instance for ordinary methods.For practical suggestions on how to design cooperative classes using super(), see guide to using super().