十五. Python基础(15)--内置函数-1

十五. Python基础(15)--内置函数-1

1 ● eval(), exec(), compile()

执行字符串数据类型的python代码 检测#import os 'import' in code

eval : 有返回值, 适用于执行计算语句, 例如eval("4+3").

exec : 没有返回值, 适用于执行流程控制语句, 例如exec(a = b if b>c else c)

complie:

code1 = 'for i in range(0,3): print(i)'

compile1 = compile(code1, '', 'exec') # 中间表示filename的参数即使没有也要有一个空字符串(其实也可以胡乱写一个字符串)

print(compile1) # <code object <module> at 0x0000000002802270, file "", line 1>

exec(compile1)

exec(compile1)

'''

0

1

2

0

1

2

'''

compile(str ,filename ,kind )

compile()将一个字符串编译为字节代码,

str是将要被编译的字符串,

filename是定义该字符串变量的文件,

kind参数指定了代码被编译的类型: eval, single, exec

'eval'指一个表达式.

'single'指单个语句,

'exec'指多个语句,

返回一个代码对象,该对象也可以被传递给eval()函数和exec语句来执行

 

预编译, 可以有效提高程序的执行效率

eval(), exec()compile()不要随便使用, 若使用, 也要做好充分的测试.

 

2 ● print()函数扩展

print(*objects, sep=' ', end=' ', file=sys.stdout, flush=False)

print("枯藤", "老树", "昏鸦", sep = ",", end = "←")

# 枯藤,老树,昏鸦←

# sep默认为一个空格" ", end默认为一个换行符

import time

for i in range(0,101,10):

     time.sleep(1) # 便于观察

     char_num = i//2     #打印多少个'*'

     #per_str = ' %d%% : %s ' % (i, '*' * char_num) if i == 100 else ' %d%% : %s'%(i,'*'*char_num)

     # 等价于:

     #per_str = ' %d%% : %s' % (i, '*' * char_num) if i != 100 else ' %d%% : %s ' % (i, '*' * char_num)

     # 或者是:

     per_str = ' %d%% : %s' % (i, '*' * char_num)

     # 但不能是:

     print(per_str,end='', flush=True)

 

# 100% : **************************************************

# 每次循环都是从头开始打印在控制台上的

f = open('print_test', 'a', encoding = 'utf-8')

print("This is a log!", file = f)

# 用print写日志文件.

 

3 ● 有关换行(line feed), 回车(carriage)

--CR(carriage return, 回车)--把光标移到所在行开头

--LF(line feed, 换行)--把光标移到下一行开头。

—CR+LF—回车+换行

 

4 ● 有关内置函数hash()

url = 'https://www.baidu.com/'

from urllib.request import urlopen

content = urlopen(url).read()

dic = {hash(url):content}

for key in dic:

    print(key)

print(hash(url))

'''

4122514777272343416

4122514777272343416

# 在运行一次, 得到:

8389699687043686450

8389699687043686450

'''

# 在python的一次执行中, 对于相同的可hash的对象, hash()函数返回的都是相同的数字(因为对象在一个生命周期内)

 

5 ● filter()函数和map()函数

# filter()函数, 类似于列表推导式

def func1(n):

     if n % 2 == 0:

         return True

li = [3,2,6,9,8]

 

print(filter(func1, li)) # filter()方法返回一个迭代器

print(list(filter(func1, li)))

 

'''

<filter object at 0x00000000025E99B0>

[2, 6, 8]

'''

def func2(n):

     if n % 2 == 0:

         return n

li2 = [3,2,6,9,8]

 

print(list(map(func2, li2)))

'''

[None, 2, 6, None, 8]

'''

 

 

def func3(n):

     return n**2

 

print(list(map(func3, li2)))

'''

[9, 4, 36, 81, 64]

'''

典型案例: 删除 None 或者空字符串

# def deter(m):

#     if m and len(m.strip()) > 0: # 注意m必须写在len(m.strip()) > 0之前, 否则会被警示None无法计算长度

#         return True

 

# 等价于:

def deter(m):

    return m and len(m.strip())

 

lst = ['test', None, '', 'str', ' ', 'END']

 

print(list(filter(deter, lst)))

'''

['test', 'str', 'END']

 

6 ● 内置函数__import__()

__import__('a')        # 导入 a.py 模块

原文地址:https://www.cnblogs.com/ArrozZhu/p/8393713.html