Python内置函数示例 (2)

返回一个描述当前全局变量的字典

>>> print(globals()) # globals 函数返回一个全局变量的字典,包括所有导入的变量。 {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, 'a': 'runoob', '__package__': None} hasattr()

函数用于判断对象是否包含对应的属性

>>>class A(object): ... bar = 1 ... >>> a = A() >>> hasattr(a,'bar') True >>> hasattr(a,'test') False hash()

返回对象的哈希值

>>>class A(object): ... bar = 1 ... >>> a = A() >>> hash(a) -2143982521 >>> help()

返回对象的帮助文档

>>>class A(object): ... bar = 1 ... >>> a = A() >>> help(a) Help on A in module __main__ object: class A(builtins.object) | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | bar = 1 >>> id()

返回对象的内存地址

>>>class A(object): ... bar = 1 ... >>> a = A() >>> id(a) 56018040 >>> input()

获取用户输入内容

>>> input() ... test 'test' >>> int()

用于将一个字符串或数字转换为整型

>>> int('14',16) 20 >>> int('14',8) 12 >>> int('14',10) 14 >>> isinstance()

来判断一个对象是否是一个已知的类型,类似 type()

>>> test = 100 >>> isinstance(test,int) True >>> isinstance(test,str) False >>> issubclass()

用于判断参数 class 是否是类型参数 classinfo 的子类

#!/usr/bin/python # -*- coding: UTF-8 -*- class A: pass class B(A): pass print(issubclass(B,A)) # 返回 True iter()

返回一个可迭代对象,sentinel可省略

>>>lst = [1, 2, 3] >>> for i in iter(lst): ... print(i) ... 1 2 3 len()

返回对象的长度

>>> dic = {'a':100,'b':200} >>> len(dic) 2 >>> list()

返回可变序列类型

>>> a = (123,'xyz','zara','abc') >>> list(a) [123, 'xyz', 'zara', 'abc'] >>> map()

返回一个将function应用于iterable中每一项并输出其结果的迭代器

>>>def square(x) : # 计算平方数 ... return x ** 2 ... >>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方 [1, 4, 9, 16, 25] >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数 [1, 4, 9, 16, 25] # 提供了两个列表,对相同位置的列表数据进行相加 >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19] max()

返回最大值

>>> max (1,2,3,4,5,6,7,8,9) 9 >>> min()

返回最小值

>>> min (1,2,3,4,5,6,7,8) 1 >>> memoryview()

返回给定参数的内存查看对象(memory view)

>>>v = memoryview(bytearray("abcefg", 'utf-8')) >>> print(v[1]) 98 >>> print(v[-1]) 103 >>> print(v[1:4]) <memory at 0x10f543a08> >>> print(v[1:4].tobytes()) b'bce' >>> next()

返回可迭代对象的下一个元素

>>> a = iter([1,2,3,4,5]) >>> next(a) 1 >>> next(a) 2 >>> next(a) 3 >>> next(a) 4 >>> next(a) 5 >>> next(a) Traceback (most recent call last): File "<pyshell#72>", line 1, in <module> next(a) StopIteration >>> object()

返回一个没有特征的新对象

>>> a = object() >>> type(a) <class 'object'> >>> open()

返回文件对象

>>>f = open('test.txt') >>> f.read() '123/123/123' pow()

base为底的exp次幂,如果mod给出,取余

>>> pow (3,1,4) 3 >>> print()

打印对象

class property()

返回property属性

class C(object): 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, "I'm the 'x' property.") range()

生成一个不可变序列

>>> range(10) range(0, 10) >>> reversed()

返回一个反向的iterator

>>> a = 'test' >>> a 'test' >>> print(list(reversed(a))) ['t', 's', 'e', 't'] >>> round()

四舍五入

>>> round (3.33333333,1) 3.3 >>> class set()

返回一个set对象,可实现去重

>>> a = [1,2,3,4,5,5,6,5,4,3,2] >>> set(a) {1, 2, 3, 4, 5, 6} >>> class slice()

返回一个表示有1range所指定的索引集的slice对象

>>> a = [1,2,3,4,5,5,6,5,4,3,2] >>> a[slice(0,3,1)] [1, 2, 3] >>> sorted()

对所有可迭代的对象进行排序操作

>>> a = [1,2,3,4,5,5,6,5,4,3,2] >>> sorted(a,reverse=True) [6, 5, 5, 5, 4, 4, 3, 3, 2, 2, 1] >>> @staticmethod

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssfdf.html