File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed
Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -952,6 +952,33 @@ file()
952952 f = open (pathname, ' rb' ) # if f.read() should return bytes
953953 # or
954954 f = open (pathname, ' rt' ) # if f.read() should return unicode text
955+ exec
956+ ~~~~
957+
958+ .. code :: python
959+
960+ # Python 2 only:
961+ exec ' x = 10'
962+
963+ # Python 2 and 3:
964+ exec (' x = 10' )
965+ .. code :: python
966+
967+ # Python 2 only:
968+ g = globals ()
969+ exec ' x = 10' in g
970+
971+ # Python 2 and 3:
972+ g = globals ()
973+ exec (' x = 10' , g)
974+ .. code :: python
975+
976+ # Python 2 only:
977+ l = locals ()
978+ exec ' x = 10' in g, l
979+
980+ # Python 2 and 3:
981+ exec (' x = 10' , g, l)
955982 execfile()
956983~~~~~~~~~~
957984
Original file line number Diff line number Diff line change 20642064 " f = open(pathname, 'rt') # if f.read() should return unicode text"
20652065 ]
20662066 },
2067+ {
2068+ "cell_type" : " markdown" ,
2069+ "metadata" : {},
2070+ "source" : [
2071+ " ### exec"
2072+ ]
2073+ },
2074+ {
2075+ "cell_type" : " code" ,
2076+ "execution_count" : null ,
2077+ "metadata" : {
2078+ "collapsed" : true
2079+ },
2080+ "outputs" : [],
2081+ "source" : [
2082+ " # Python 2 only:\n " ,
2083+ " exec 'x = 10'\n " ,
2084+ " \n " ,
2085+ " # Python 2 and 3:\n " ,
2086+ " exec('x = 10')"
2087+ ]
2088+ },
2089+ {
2090+ "cell_type" : " code" ,
2091+ "execution_count" : null ,
2092+ "metadata" : {
2093+ "collapsed" : true
2094+ },
2095+ "outputs" : [],
2096+ "source" : [
2097+ " # Python 2 only:\n " ,
2098+ " g = globals()\n " ,
2099+ " exec 'x = 10' in g\n " ,
2100+ " \n " ,
2101+ " # Python 2 and 3:\n " ,
2102+ " g = globals()\n " ,
2103+ " exec('x = 10', g)"
2104+ ]
2105+ },
2106+ {
2107+ "cell_type" : " code" ,
2108+ "execution_count" : null ,
2109+ "metadata" : {
2110+ "collapsed" : true
2111+ },
2112+ "outputs" : [],
2113+ "source" : [
2114+ " # Python 2 only:\n " ,
2115+ " l = locals()\n " ,
2116+ " exec 'x = 10' in g, l\n " ,
2117+ " \n " ,
2118+ " # Python 2 and 3:\n " ,
2119+ " exec('x = 10', g, l)"
2120+ ]
2121+ },
2122+ {
2123+ "cell_type" : " markdown" ,
2124+ "metadata" : {},
2125+ "source" : [
2126+ " But note that Py3's `exec()` is less powerful (and less dangerous) than Py2's `exec` statement."
2127+ ]
2128+ },
20672129 {
20682130 "cell_type" : " markdown" ,
20692131 "metadata" : {},
You can’t perform that action at this time.
0 commit comments