Python临时笔记

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值