mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 18:42:25 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -6,7 +6,7 @@ Python会将所有 `.py` 结尾的文件认定为Python代码文件,考虑下
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%writefile ex1.py
|
||||
|
||||
PI = 3.1416
|
||||
@@ -22,7 +22,7 @@ print sum(w), PI
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting ex1.py
|
||||
|
||||
```
|
||||
@@ -31,12 +31,12 @@ Overwriting ex1.py
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
%run ex1.py
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
6 3.1416
|
||||
|
||||
```
|
||||
@@ -45,26 +45,26 @@ In [2]:
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
import ex1
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
6 3.1416
|
||||
|
||||
```
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
ex1
|
||||
|
||||
```
|
||||
|
||||
Out[4]:
|
||||
|
||||
```
|
||||
```py
|
||||
<module 'ex1' from 'ex1.py'>
|
||||
```
|
||||
|
||||
@@ -72,7 +72,7 @@ Out[4]:
|
||||
|
||||
`ex1.py` 中所有的变量都被载入了当前环境中,不过要使用
|
||||
|
||||
```
|
||||
```py
|
||||
ex1.变量名
|
||||
```
|
||||
|
||||
@@ -80,32 +80,32 @@ ex1.变量名
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
print ex1.PI
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
3.1416
|
||||
|
||||
```
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
ex1.PI = 3.141592653
|
||||
print ex1.PI
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
3.141592653
|
||||
|
||||
```
|
||||
|
||||
还可以用
|
||||
|
||||
```
|
||||
```py
|
||||
ex1.函数名
|
||||
```
|
||||
|
||||
@@ -113,12 +113,12 @@ ex1.函数名
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
print ex1.sum([2, 3, 4])
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
9
|
||||
|
||||
```
|
||||
@@ -129,7 +129,7 @@ print ex1.sum([2, 3, 4])
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
import ex1
|
||||
|
||||
```
|
||||
@@ -138,19 +138,19 @@ import ex1
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
reload(ex1)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
6 3.1416
|
||||
|
||||
```
|
||||
|
||||
Out[9]:
|
||||
|
||||
```
|
||||
```py
|
||||
<module 'ex1' from 'ex1.pyc'>
|
||||
```
|
||||
|
||||
@@ -158,7 +158,7 @@ Out[9]:
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('ex1.py')
|
||||
|
||||
@@ -172,7 +172,7 @@ os.remove('ex1.py')
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%writefile ex2.py
|
||||
|
||||
PI = 3.1416
|
||||
@@ -200,7 +200,7 @@ if __name__ == '__main__':
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing ex2.py
|
||||
|
||||
```
|
||||
@@ -209,12 +209,12 @@ Writing ex2.py
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
%run ex2.py
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
test passed.
|
||||
|
||||
```
|
||||
@@ -223,7 +223,7 @@ test passed.
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
import ex2
|
||||
|
||||
```
|
||||
@@ -232,14 +232,14 @@ import ex2
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
ex2.PI
|
||||
|
||||
```
|
||||
|
||||
Out[14]:
|
||||
|
||||
```
|
||||
```py
|
||||
3.1416
|
||||
```
|
||||
|
||||
@@ -247,7 +247,7 @@ Out[14]:
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
import ex2 as e2
|
||||
e2.PI
|
||||
|
||||
@@ -255,7 +255,7 @@ e2.PI
|
||||
|
||||
Out[15]:
|
||||
|
||||
```
|
||||
```py
|
||||
3.1416
|
||||
```
|
||||
|
||||
@@ -265,7 +265,7 @@ Out[15]:
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
from ex2 import add, PI
|
||||
|
||||
```
|
||||
@@ -274,14 +274,14 @@ from ex2 import add, PI
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
add(2, 3)
|
||||
|
||||
```
|
||||
|
||||
Out[17]:
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
```
|
||||
|
||||
@@ -289,7 +289,7 @@ Out[17]:
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
from ex2 import *
|
||||
add(3, 4.5)
|
||||
|
||||
@@ -297,7 +297,7 @@ add(3, 4.5)
|
||||
|
||||
Out[18]:
|
||||
|
||||
```
|
||||
```py
|
||||
7.5
|
||||
```
|
||||
|
||||
@@ -307,7 +307,7 @@ Out[18]:
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('ex2.py')
|
||||
|
||||
@@ -325,7 +325,7 @@ foo/
|
||||
|
||||
这意味着 foo 是一个包,我们可以这样导入其中的内容:
|
||||
|
||||
```
|
||||
```py
|
||||
from foo.bar import func
|
||||
from foo.baz import zap
|
||||
|
||||
|
||||
Reference in New Issue
Block a user