X Tutup
Skip to content

Commit 7edf24b

Browse files
committed
module and package
1 parent 2dc5fad commit 7edf24b

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

219.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,52 @@ Python不是一个封闭的体系,是一个开放系统。开放系统的最
191191
##模块中的'__all__'
192192

193193
上面的模块虽然比较简单,但是已经显示了编写模块和在程序中导入模块的基本方式。在实践中,所编写的模块也许更复杂一点,比如,我写了这么一个模块,并把其文件命名为21901.py
194-
http://python.jobbole.com/81187/
194+
http://python.jobbole.com/81187/mZ
195+
>>> import sys
196+
>>> sys.path.append("~/Documents/StarterLearningPython/2code/pp.py")
197+
>>> import pp
198+
>>> pp.public_variable
199+
'Hello, I am a public variable.'
200+
>>> pp._private_variable
201+
'Hi, I am a private variable.'
202+
>>> from pp import *
203+
>>> _private_variable
204+
Traceback (most recent call last):
205+
File "<stdin>", line 1, in <module>
206+
NameError: name '_private_variable' is not defined
207+
>>> public_variable
208+
'Hello, I am a public variable.'
209+
>>> public_teacher()
210+
I am a public teacher, I am from JP.
211+
>>> _private_teacher()
212+
Traceback (most recent call last):
213+
File "<stdin>", line 1, in <module>
214+
NameError: name '_private_teacher' is not defined
215+
>>> import pp
216+
>>> pp.public_teacher()
217+
I am a public teacher, I am from JP.
218+
>>> pp._private_teacher()
219+
I am a private teacher, I am from CN.
220+
221+
>>> import sys
222+
>>> sys.path.append("~/Documents/StarterLearningPython/2code/pp.py")
223+
>>> from pp import *
224+
>>> dir(pp)
225+
Traceback (most recent call last):
226+
File "<stdin>", line 1, in <module>
227+
NameError: name 'pp' is not defined
228+
>>> public_variable
229+
Traceback (most recent call last):
230+
File "<stdin>", line 1, in <module>
231+
NameError: name 'public_variable' is not defined
232+
>>> _private_variable
233+
'Hi, I am a private variable.'
234+
>>> public_teacher()
235+
I am a public teacher, I am from JP.
236+
>>> _private_teacher()
237+
Traceback (most recent call last):
238+
File "<stdin>", line 1, in <module>
239+
NameError: name '_private_teacher' is not defined
195240

196241

197242
##`__init__.py`方法
@@ -202,4 +247,4 @@ http://python.jobbole.com/81187/
202247

203248
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:错误和异常(3)](./218.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:标准库(1)](./220.md)
204249

205-
如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。
250+
如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。

2code/21901.py renamed to 2code/pp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# /usr/bin/env python
22
# coding:utf-8
33

4+
__all__ = ['_private_variable', 'public_teacher']
5+
46
public_variable = "Hello, I am a public variable."
57
_private_variable = "Hi, I am a private variable."
68

2code/pp.pyc

563 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
X Tutup