repr,eval,exec,str区别

本文介绍了Python中用于处理对象和字符串的几种内置函数:repr(), eval(), exec(), str()。通过实例展示了这些函数如何帮助开发者更好地理解和操作数据。特别是对repr()和str()的区别进行了详细解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数作用
repr:repretension 生成展示对象的字符串
eval:evaluetion求字符串里面的值
exec: 执行字符串内的命令或者变量
str:字符串化
返回类型
repr:字符串
eval:字符串或者整形值
exec: 返回函数执行后的结果
str:字符串

repr is for developers, str is for customers.
the goal of repr is to be unambiguous and str is to be readable.

>>> import datetime
>>> today = datetime.datetime.now()
>>> str(today)
'2012-03-14 09:21:58.130922'
>>> repr(today)
'datetime.datetime(2012, 3, 14, 9, 21, 58, 130922)'
''' Hints: Flag在环境变量中 ''' from typing import Optional import pydash import bottle __forbidden_path__=['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__wrapped__', "Optional","func","render", ] __forbidden_name__=[ "bottle" ] __forbidden_name__.extend(dir(globals()["__builtins__"])) def setval(name:str, path:str, value:str)-> Optional[bool]: if name.find("__")>=0: return False for word in __forbidden_name__: if name==word: return False for word in __forbidden_path__: if path.find(word)>=0: return False obj=globals()[name] try: pydash.set_(obj,path,value) except: return False return True @bottle.post('/setValue') def set_value(): name = bottle.request.query.get('name') path=bottle.request.json.get('path') if not isinstance(path,str): return "no" if len(name)>6 or len(path)>32: return "no" value=bottle.request.json.get('value') return "yes" if setval(name, path, value) else "no" @bottle.get('/render') def render_template(): path=bottle.request.query.get('path') if path.find("{")>=0 or path.find("}")>=0 or path.find(".")>=0: return "Hacker" return bottle.template(path) bottle.run(host='0.0.0.0', port=8000)''' Hints: Flag在环境变量中 ''' from typing import Optional import pydash import bottle __forbidden_path__=['__annotations__', '__call__', '__class__', '__closure__',
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值