python3内置函数
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()
Asrepr()
, return a string containing a printable representation of an object, but escape the non-ASCII
characters in the string returned byrepr()
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 class
dict对象用来,创建字典类
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().
python3内置函数的更多相关文章
- python3内置函数大全(顺序排列)
python3内置函数大全 内置函数 (1)abs(), 绝对值或复数的模 1 print(abs(-6))#>>>>6 (2)all() 接受一个迭代器,如果迭代器的所有 ...
- python3内置函数大全
由于面试的时候有时候会问到python的几个基本内置函数,由于记不太清,就比较难受,于是呕心沥血总结了一下python3的基本内置函数 Github源码: https://github. ...
- Python3内置函数、各数据类型(int/str/list/dict/set/tuple)的内置方法快速一览表
Python3内置函数 https://www.runoob.com/python3/python3-built-in-functions.html int https://www.runoob.co ...
- python3内置函数详解
内置函数 注:查看详细猛击这里 abs() 对传入参数取绝对值 bool() 对传入参数取布尔值, None, 0, "",[],{},() 这些参数传入bool后,返回False ...
- python 之 python3内置函数
一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内 ...
- Python3 内置函数补充匿名函数
Python3 匿名函数 定义一个函数与变量的定义非常相似,对于有名函数,必须通过变量名访问 def func(x,y,z=1): return x+y+z print(func(1,2,3)) 匿名 ...
- python3 内置函数
'''iter()和next()'''# lst = [1, 2, 3]# it = iter(lst)# print(it.__next__())# print(it.__next__())# pr ...
- python3 内置函数详解
内置函数详解 abs(x) 返回数字的绝对值,参数可以是整数或浮点数,如果参数是复数,则返回其大小. # 如果参数是复数,则返回其大小. >>> abs(-25) 25 >&g ...
- python3 内置函数enumerate
一.简介: 该函数在字面上是枚举.列举的意思,用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列, 同时列出数据和数据下标,一般用在 for 循环当中,可同时得到数据对象的值及对应的 ...
随机推荐
- CSS2中基本属性的介绍
这是继上一篇的选择器的总结,对css2基本属性的小结!
- 自定义圆饼(利用贝塞尔曲线和CGContext类完成)
-(void)drawRect:(CGRect)rect{ CGFloat w = self.bounds.size.width; CGFloat h = self.bounds.size.heigh ...
- 【Java】模板方法模式
今天介绍的是模板方法模式~ 模板方法模式,由父类定制总体的框架,具体的细节由子类实现. 一般来说,模板方法中有3类方法: 抽象方法,父类声明方法待子类具体实现.比如例子的validate.save.u ...
- JS判断日期是否在同一个星期内,和同一个月内
今天要用到判断日期是否在同一个星期内和是否在同一个月内,在网上找了好一会儿也没找到合适的,然后自己写了一个方法来处理这个问题,思路就不详细介绍了,直接附上代码,自己测试了一下 没有问题,若有问题请在评 ...
- 【转】关于 mate viewport属性的具体说明!
什么是Viewport 手机浏览器是把页面放在一个虚拟的"窗口"(viewport)中,通常这个虚拟的"窗口"(viewport)比屏幕宽,这样就不用把每个网页 ...
- 剑指offer三: 斐波拉契数列
斐波拉契数列是指这样一个数列: F(1)=1; F(2)=1; F(n)=F(n-1)+F(n); public class Solution { public int Fibonacci(int n ...
- treap 模版
struct Treap { struct node { node *son[]; int key,siz,wei,cnt; node(int _key,node *f) { son[]=son[]= ...
- Linux系统移植(1) ------搭建交叉编译环境
本人的开发环境是ubuntu12.05的64版本,运行在11.00的虚拟机上.首先说明为什么需要搭建交叉编译环境.我们知道,我们的开发一般在PC机上,是基于X86架构的,而我们的开发板却是基于ARM架 ...
- js之事件冒泡和事件捕获
(1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...
- redux介绍与入门
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Helvetica } p.p2 { margin: 0.0px 0.0px 0.0px 0. ...