eval
eval(expression[, globals[, locals]])函数用来执行expression并返回结果。expression是一个字符串,globals和locals是可选的。如果给值得话globals必须是一个dict,locals可以是任意形式的map对象。如果locals不设置则默认与globals相同。如果都不设置则使用调用eval的环境。
该函数还可以用于执行任意的code object(例如compile创建的)。如果使用"exec"编译的则结果返回None。compile在内置函数三已经介绍过。
测试代码:
def test_eval():
cmd = "1 + 2"
r = eval(cmd)
print("r: ", r)
x = 4
cmd1 = "x+3"
r = eval(cmd1)
print("r: ", r)
code_str = "3+4"
code = compile(code_str, "<string>", "single")
eval(code)
test_eval()
测试结果:
r: 3
r: 7
7
exec
exec(object[, globals[, locals]])函数用来动态执行python代码。object可以是一个string或者一个code object。返回值是None。
python2中有一个execfile的内置函数,exec不是内置函数,在python3中被删除了execfile而把exec变成了内置函数。
测试代码:
def test_exec():
code_str = "a = 1\r\nb=2\r\nc = a + b\r\nprint('c = ', c)"
exec(code_str)
code = compile("", filename="./test_dir.py", mode="exec")
exec(code)
code_str = "3+4"
exec(code_str)
test_exec()
测试输出:
c = 3
file
file()函数用于返回一个file对象,但是在python3中被删除了, 可以使用open函数实现。这里不再介绍。
filter
filter(function, iterable)方法用于过滤iterabal指定的可迭代序列,并返回符合function条件的新迭代器对象。iterable可以是一个序列,支持迭代的容器或者一个迭代器对象。如果function为None的话则返回所有值。
测试代码:
def test_filter():
def my_filter(x):
return x > 5
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
new_nums = filter(my_filter, nums)
print("new nums: ", new_nums)
for a in new_nums:
print("new nums: ", a)
new_nums1 = filter(None, nums)
print("new nums1: ", new_nums1)
for a in new_nums1:
print("new nums1: ", a)
test_filter()
new nums: <filter object at 0x7f97934c6210>
new nums: 6
new nums: 7
new nums: 8
new nums: 9
new nums1: <filter object at 0x7f97934c62d0>
new nums1: 1
new nums1: 2
new nums1: 3
new nums1: 4
new nums1: 5
new nums1: 6
new nums1: 7
new nums1: 8
new nums1: 9
float
class float([x])将x转换为一个float类型数,x可以是一个string或者一个数字。如果是string则string中必须包含一个十进制数。可以带正负符号和空格。
测试代码:
def test_float():
num = float( 123 )
print("num: ", num)
num1 = float(" -321.11 ")
print("num1: ", num1)
#num2 = float("123aa") # error:ValueError: could not convert string to float: '123aa'
#print("num2: ", num2)
num3 = float("inf")
print("num3: ", num3)
num4 = float('1e-003')
print("num4: ", num4)
test_float()
测试结果:
num: 123.0
num1: -321.11
num3: inf
num4: 0.001
format
format(value[, format_spec])函数用来格式话字符串,format可以接受不限个数的参数,参数顺序可以不按照顺序。
测试代码:
def test_format():
output = format("test; %s ;test" % "hello world")
print("output: ", output)
output1 = "test; {} {} ;test".format("hello", "world")
print("output1: ", output1)
output2 = "test: {1} {0} {1}".format("hello", "world")
print("output2: ", output2)
output3 = "test: {first} {second} {first}".format(second="hello", first="world")
print("output3: ", output3)
test_cls = TestClass()
output4 = "test; {0.num} ;test".format(test_cls)
print("output4: ", output4)
num_str = "{:.2f}".format(3.1415926)
print("num_str: ", num_str)
test_format()
测试结果:
output: test; hello world ;test
output1: test; hello world ;test
output2: test: world hello world
output3: test: world hello world
output4: test; 6 ;test
num_str: 3.14
frozenset
class frozenset([iterable]) 创建一个不可变的集合对象。可选参数是一个可迭代的对象。
测试代码:
def test_frozenset():
out1 = frozenset(range(10))
print("out1: ", out1)
out2 = frozenset([1, 2, 3, 4, 5, 6, 7])
print("out2: ", out2)
out3 = frozenset({0: "zero", 1: "one", 2: "two"})
print("out3: ", out3)
out4 = frozenset()
print("out4: ", out4)
测试结果:
out1: frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
out2: frozenset({1, 2, 3, 4, 5, 6, 7})
out3: frozenset({0, 1, 2})
out4: frozenset()
hash
hash(object) 用于获取取一个对象(字符串或者数值等)的哈希值。哈希值是一个整数。可用于在查找字典键值时快速比较。相等的数值得到的哈希值也相同,即使类别不同,例如1和1.0。请注意 hash() 会根据主机的位宽截断返回值。
测试代码:
def test_hash():
print("hash of 100", hash(100))
print("hash of 100.0", hash(100.0))
print("hash of abcdef", hash("abcdef"))
print("hash of ;;;;;", hash(";;;;;"))
test_hash()
测试结果:
hash of 100 100
hash of 100.0 100
hash of abcdef -3611082344017052973
hash of ;;;;; -1646571678035759429
help
help([object])用于查看函数或模块的说明。
测试代码:
def test_help():
help(sys)
help(help)
测试结果:
略
hex
hex(x)将一个整数转换为十六进制,以0x开头。
测试代码:
def test_hex():
print("hex of 255:", hex(255))
print("hex of -132:", hex(-132))
print("hex of 0:", hex(0))
test_hex()
测试结果:
hex of 255: 0xff
hex of -132: -0x84
hex of 0: 0x0