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




原创粉丝点击