Three special statements: pass, del, and exec
来源:互联网 发布:网络开发技术 编辑:程序博客网 时间:2024/06/08 03:44
1)Nothing Happened by pass
Sometimes you need to do nothing. This may not be very often, but when it happens, it's good to know that you have the pass statement:
>>> pass>>>
another example:
if name == 'Ralph Auldus Melish': print 'Welcome!'elif name == 'Enid': # Not finished yet...elif name == 'Bill Gates': print 'Access Denied'
This code won't run because an empty block is illegal in Python. To fix this, simply add a pass statement to the middle block:
if name == 'Ralph Auldus Melish': print 'Welcome!'elif name == 'Enid': # Not finished yet... passelif name == 'Bill Gates': print 'Access Denied'
2)Deleting with del
In general, Python deletes objects that you don't use anymore (because you no longer refer to them through any variables or parts of your data structures):
>>> scoundrel = {'age': 42, 'first name': 'Robin', 'last name': 'of Locksley'}>>> robin = scoundrel>>> scoundrel{'age': 42, 'first name': 'Robin', 'last name': 'of Locksley'}>>> robin{'age': 42, 'first name': 'Robin', 'last name': 'of Locksley'}>>> scoundrel = None>>> robin{'age': 42, 'first name': 'Robin', 'last name': 'of Locksley'}>>> robin = None
when I assign None to robin as well, the dictionary suddenly floats around in the memory of the computer with no name attached to it. There is no way I can retrieve it or use it, so the Python interpreter (in its infinite wisdom) simply deletes it. (This is called garbage collection.) Note that I could have used any value other than None as well. The dictionary would be just as gone.
Another way of doing this is to use the del statement .This not only removes a reference to an object, but it also removes the name itself:
>>> x = 1>>> del x>>> xTraceback (most recent call last):File "<pyshell#255>", line 1, in ?xNameError: name 'x' is not defined
This may seem easy, but it can actually be a bit tricky to understand at times. For instance, in the following example, x and y refer to the same list:
>>> x = ["Hello", "world"]>>> y = x>>> y[1] = "Python">>> x['Hello', 'Python']>>> del x>>> y['Hello', 'Python']
You might assume that by deleting x, you would also delete y, but that is not the case. Why is this? x and y referred to the same list, but deleting x didn’t affect y at all. The reason for this is that you delete only the name, not the list itself (the value). In fact, there is no way to delete values in Python—and you don’t really need to, because the Python interpreter does it by itself whenever you don’t use the value anymore.
See another example with del about list:
>>> params = {'job': 'language', 'name': 'Python'}>>> del params['job']>>> params{'name': 'Python'}
as you see, del delete the name "jobs" and its reference to the value "language".
3)Executing and Evaluating Strings with exec and eval
exec:
sometimes you may want to create Python code "on the fly"and execute it as a statement or evaluate it as an expression. This may border on dark magic at times—consider yourself warned.
>>> exec "print 'Hello, world!'"Hello, world!
Caution:Here, you learn to execute Python code stored in a string. This is a potential security hole of great dimensions. If you execute a string where parts of the contents have been supplied by a user, you have little or no control over what code you are executing. This is especially dangerous in network applications, such as Common Gateway Interface (CGI) scripts.
eval:
A built-in function that is similar to exec is eval (for “evaluate”). Just as exec executes a series of Python statements, eval evaluates a Python expression (written in a string) and returns the resulting value. (exec doesn’t return anything because it is a statement itself.)
>>> eval(raw_input("Enter an arithmetic expression: "))Enter an arithmetic expression: 6 + 18 * 242
- Three special statements: pass, del, and exec
- python中的pass,del,exec,eval语句
- python中的pass,del,exec,eval语句
- Which three statements are correct about temporary tables? (Choose three.) A. Indexes and views can
- pytho中的pass和del
- python学习笔记——pass del
- 133. Which three statements are true? (Choose three.)
- 42.Which three statements are true regarding views? (Choose three.)
- 135.Which three statements are true regarding subqueries? (Choose three.)
- 【Python】python学习笔记——pass del
- python中关键字del、assert、pass的作用
- ORACLE ONE PASS AND MULTI PASS
- hibernte 批量Update and del
- Statements and Declarations in Expressions
- JDBC - Statements, PreparedStatement and CallableStatement
- Process ID and special processes
- Special Characters and XML Strings
- Matlab special usage : @ and Struct
- 專案參考X86跟X64的dll
- this指针再探讨:究竟绑定谁?
- Python Import机制备忘-模块搜索路径(sys.path)、嵌套Import、package Import
- Training Tesseract OCR 3.0.1
- Android应用开发--MP3音乐播放器代码实现(二)
- Three special statements: pass, del, and exec
- 關於互斥鎖
- 系统中使用堆和栈管理内存的区别
- 备忘录模式(Memento)
- cocos2d-x 菜鸟学习笔记八(界面控件之CCControl控件)
- 在python中,如何获取脚本文件所在路径
- ubuntu最常用的10个快捷键
- Ubuntu下Git以及Github使用
- fms系列5 时时视频聊天