函数(六)——内置函数大全(一)

一、内置函数

1)作用域相关

基于字典的形式获取局部变量和全局变量

globals()——获取全局变量的字典

locals()——获取执行本方法所在命名空间内的局部变量的字典

2)迭代器/生成器相关

next:

迭代器.__next__()

next(迭代器)

iter:

迭代器 = iter(可迭代的)

迭代器 = 可迭代的.__iter__()

range:

range(10)

range(1,11)
print('__next__' in dir(range(1,11,2)))

3)其他

字符串类型代码的执行

eval(): 将字符串类型的代码执行并返回结果

print(eval('1+2+3+4'))

exec():将自字符串类型的代码执行

print(exec("1+2+3+4"))
exec("print('hello,world')")
code = '''
import os 
print(os.path.abspath('.'))
'''
code = '''
print(123)
a = 20
print(a)
'''
a = 10
exec(code,{'print':print},)
print(a)

指定global参数

compile:将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值。

参数说明:   

1. 参数source:字符串或者AST(Abstract Syntax Trees)对象。即需要动态执行的代码段。  

2. 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。当传入了source参数时,filename参数传入空字符即可。  

3. 参数model:指定编译代码的种类,可以指定为 ‘exec’,’eval’,’single’。当source中包含流程语句时,model应指定为‘exec’;当source中只包含一个简单的求值表达式,model应指定为‘eval’;当source中包含了交互式命令语句,model应指定为'single'。

>>> #流程语句使用exec
>>> code1 = 'for i in range(0,10): print (i)'
>>> compile1 = compile(code1,'','exec')
>>> exec (compile1)
3
7


>>> #简单求值表达式用eval
>>> code2 = '1 + 2 + 3 + 4'
>>> compile2 = compile(code2,'','eval')
>>> eval(compile2)


>>> #交互语句用single
>>> code3 = 'name = input("please input your name:")'
>>> compile3 = compile(code3,'','single')
>>> name #执行前name变量不存在
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    name
NameError: name 'name' is not defined
>>> exec(compile3) #执行时显示交互命令,提示输入
please input your name:'pythoner'
>>> name #执行后name变量有值
"'pythoner'"

输入输出

input() 输入

s = input("请输入内容 : ")  #输入的内容赋值给s变量
print(s)  #输入什么打印什么。数据类型是str

print() 输出

def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
    """
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    file:  默认是输出到屏幕,如果设置为文件句柄,输出到文件
    sep:   打印多个值之间的分隔符,默认为空格
    end:   每一次打印的结尾,默认为换行符
    flush: 立即把内容输出到流文件,不作缓存
    """

print源码剖析
print源码剖析
f = open('tmp_file','w')
print(123,456,sep=',',file = f,flush=True)
file关键字的说明
import time
for i in range(0,101,2):  
     time.sleep(0.1)
     char_num = i//2      #打印多少个'*'
     per_str = '\r%s%% : %s\n' % (i, '*' * char_num) if i == 100 else '\r%s%% : %s'%(i,'*'*char_num)
     print(per_str,end='', flush=True)
#小越越  : \r 可以把光标移动到行首但不换行

打印进度条
打印进度条

内存相关

id(o) o是参数,返回一个变量的内存地址

hash(o) o是参数,返回一个可hash变量的哈希值,不可hash的变量被hash之后会报错。

t = (1,2,3)
l = [1,2,3]
print(hash(t))  #可hash
print(hash(l))  #会报错

'''
结果:
TypeError: unhashable type: 'list'
'''

hash实例
hash实例

hash函数会根据一个内部的算法对当前可hash变量进行处理,返回一个int数字。

*每一次执行程序,内容相同的变量hash值在这一次执行过程中不会发生改变。

数据类型相关

type(o) 返回变量o的数据类型

文件操作相关

open()  打开一个文件,返回一个文件操作符(文件句柄)

操作文件的模式有r,w,a,r+,w+,a+ 共6种,每一种方式都可以用二进制的形式操作(rb,wb,ab,rb+,wb+,ab+)

可以用encoding指定编码.

模块相关

__import__导入一个模块

import time
导入模块
os = __import__('os')
print(os.path.abspath('.'))
__import__

帮助方法

在控制台执行help()进入帮助模式。可以随意输入变量或者变量的类型。输入q退出

或者直接执行help(o),o是参数,查看和变量o有关的操作。。。

和调用相关

callable(o),o是参数,看这个变量是不是可调用。

如果o是一个函数名,就会返回True

def func():pass
print(callable(func))  #参数是函数名,可调用,返回True
print(callable(123))   #参数是数字,不可调用,返回False
callable实例

查看参数所属类型的所有内置方法

print(dir(list))  #查看列表的内置方法
print(dir(int))  #查看整数的内置方法
查看某变量/数据类型的内置方法

4)基础数据类型相关

和数字相关

数字——数据类型相关:bool,int,float,complex

数字——进制转换相关:bin,oct,hex

数字——数学运算:abs,divmod,min,max,sum,round,pow

posted @ 2018-01-04 19:54  带带大师兄丶  阅读(237)  评论(0编辑  收藏  举报
欢迎第 Free Counters个访客