Python临时笔记

动态语言python的特性

python2  输入一个匿名函数直接执行

# coding=utf-8
def Test(a, b, func):
    result = func(a, b)
    return result

func_new = input("please input a fun::")    #please input a fun::lambda x,y:x+y
print(Test(10, 20, func_new))#30

python3 会把input输入进去的东西全部转换成字符串  因此需要eval方法转换 

def Test(a, b, func):
    result = func(a, b)
    return result

func_new = input("please input a fun::")  # python3中input接受的值全部变成string类型   please input a fun::lambda x,y:x+y
func_new = eval(func_new)  # 执行字符串的python语句  
print(Test(10, 20, func_new)) #30

 python对bytes和string 进行了明确的区分 需要转换的时候的方法是

s.encode(encoding) #string-> bytes   encoding是具体的编码格式 例如utf-8 ascii
b.decode(encoding) #bytes -> str
原文地址:https://www.cnblogs.com/kanekiken/p/7354984.html