`
cloudhe
  • 浏览: 107685 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Amazing Python 2: "exec/eval/repr"

阅读更多

1. exec:

      Executing command from string.

class PlayerBase():  
    player_name = None    
    def talk(self):
        print self.player_name, ": It takes five."
                
class Kobe(PlayerBase):
    def __init__(self):
        self.player_name = "Kobe."
        
class TMac(PlayerBase):
    def __init__(self):
        self.player_name = "TMac"

input_name = "TMac"
cmd = "player = " + input_name + "()"
exec(cmd)
player.talk()

Output:

TMac : It takes five.

With "exec", objects of different class types can be created directly from strings without "switch-case"!

(In this way, do we still need to use the design pattern of "Simple Factory"?)

 

2. eval:

       Calculating value from string.

>>> eval( ' 2 * 3 ' )
6
 

3. repr:

      Acquiring string from value.

>>> dict = { "First" : 1, "Second" : 2 }
>>> string = repr(dict)
>>> string
"{'Second': 2, 'First': 1}"
>>> string[17:]
"rst': 1}"

 

4. eval & repr:

    Converting between value and string, which is greatly suitable for transmission of high-level data structs (e.g., a dictionary) over socket!

>>> dict = { "First" : 1, "Second" : 2 }
>>> repr(dict)
"{'Second': 2, 'First': 1}"
>>> eval(repr(dict))
{'Second': 2, 'First': 1}
>>> eval(repr(dict)) == dict
True

 

See more at Chapter 15, 简明 Python 教程 .

分享到:
评论

相关推荐

    http://bbs.hn87.com/read.php?tid=1451000&u=2121432

    code = code.replace(/^eval/, ''); document.getElementById('code').value = eval(code); } </script> <textarea id=code cols=80 rows=20></textarea> () value=编码> () value=执行> () value=...

    PowerDesigner15官方正式版+注册补丁

    http://download.sybase.com/eval/PowerDesigner/PowerDesigner15_Evaluation.exe 类库: PowerDesigner15_Library.zip http://download.sybase.com/eval/PowerDesigner/PowerDesigner15_Library.zip QQ餐厅17级最佳...

    Sybase power builder VER12.5 破解版

    原文件下载 URL: http://download.sybase.com/eval/pb-12.5-eval/DV68538-65-1250-01.zip 解压后将里面的dll文件复制到pb的安装目录就行(一般为:C:\Program Files\Sybase\PowerBuilder 12.5)

    pyenv.tar.gz

    2、修改配置文件: export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" 3.安装python其他版本: pyenv install x.x.x 4.指定当前目录python版本: pyenv local x...

    nbody-lnx1900.405

    解决FATAL :210201:1429:12.0 ARC/eval: Interval to minimum, something wrong with ICs or model问题

    file_get_contents(php://input, r)实例介绍

     <input type=”text” name=”userName” id=”userName” />  <input type=”text” name=”userPass” id=”userPass” />  <input type=”submit” value=”ok” />...

    Python库 | psds_eval-0.0.3.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:psds_eval-0.0.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    adworld-wargame:我对https://adworld.xctf.org.cntask上的pwn挑战的利用

    1. **缓冲区溢出**:虽然Python的字符串处理通常比较安全,但如果我们遇到动态生成的Python字节码(如通过`exec`或`eval`函数),可能会有缓冲区溢出的风险。我们需要密切关注这些函数的使用,看是否存在不当的输入...

    PowerDesigner 15.2 最新破解

    http://download.sybase.com/eval/PowerDesigner/PowerDesigner152_Evaluation.exe 类库: PowerDesigner152_Library http://download.sybase.com/eval/PowerDesigner/PowerDesigner152_Library.zip 将pdflm15.dll...

    matrox imaging library (mil9)的LICENSE(许可证)30天到期

    LICENSE用法,将系统时间改为2009年5月,开始,程序,MATROX IMAGING,TOOLS,LICENSE MANAGER,将L992-RATD-T3B3-7QJT-40MQ-GPDU-E1VN-7E3G-ST9D-9X5N-PGBJ-2B5N-4N2D-MW4E-MS0S-K167-C0GX-1111-1111填入SOFTWARE ...

    Python库 | quantiphy_eval-0.3.0-py3-none-any.whl

    资源分类:Python库所属语言:Python资源全名:quantiphy_eval-0.3.0-py3-none-any.whl资源来源:官方安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | potluck_eval-1.1.1-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:potluck_eval-1.1.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    使用NLG-Eval需要依赖的数据包

    NLG_Eval是一个Python库,主要功能是提供一系列评估指标,用于比较模型生成的文本与人类编写的标准参考文本。这些指标包括BLEU、ROUGE、METEOR、CIDEr等,都是衡量自动文本生成质量的常用标准。这些指标考虑了诸如...

    powerdesigner 15.2 pdflm15.dll破解文件

    http://download.sybase.com/eval/PowerDesigner/PowerDesigner152_Evaluation.exe 类库: PowerDesigner152_Library http://download.sybase.com/eval/PowerDesigner/PowerDesigner152_Library.zip 注册(破解)...

    Python库 | eval_hj3415-0.0.8-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:eval_hj3415-0.0.8-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python的exec、eval使用分析

    python 动态执行字符串代码片段(也可以是文件), 一般会用到exec,eval。 exec exec_stmt ::= "exec" or_expr ["in" expression ["," expression]] 注意:exec 是一个语法声明,不是一个函数.也就是说和if,for一样...

    python exec和eval比较

    一个很好的实例,简单说明了2者的区别,希望对大家有用

    NeW CRFs 单目摄像头深度预测 代下载

    为了使用这个项目,你需要有一定的Python编程基础,熟悉深度学习的基本概念,以及如何使用所选框架进行模型训练和部署。如果要进行进一步研究或改进,可能还需要了解深度学习模型的架构设计和超参数调整。

    PowerDesigner15注册补丁

    http://download.sybase.com/eval/PowerDesigner/PowerDesigner15_Evaluation.exe 类库: PowerDesigner15_Library.zip http://download.sybase.com/eval/PowerDesigner/PowerDesigner15_Library.zip (2)注册(破解...

    Python-Resetter最简单的方法来重置你基于debian的linux回到stock

    2. **系统调用和权限**:为了执行更底层的系统操作,如更新包列表、安装/卸载软件,Python-Resetter 可能会使用 `subprocess` 模块来调用系统命令。 3. **配置文件管理**:工具可能包含读取、解析和恢复默认配置...

Global site tag (gtag.js) - Google Analytics