模块内置变量

a = 2
b = 3
d = 4

infos = dir()
print(infos)
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'd']

可以看到dir()返回的是模块中所有的变量,双下划线是系统内置的变量

print("name:"+__name__)
# print("package:"+__package__)
print("file:"+__file__)
# print("doc:"+__doc__)
├── __init__.py
├── __pycache__
│   ├── c1.cpython-38.pyc
│   └── c2.cpython-38.pyc
├── c1.py
├── c2.py
├── c3.py
├── c4.py
├── p
│   ├── __pycache__
│   │   ├── p1.cpython-38.pyc
│   │   ├── p2.cpython-38.pyc
│   │   └── p4.cpython-38.pyc
│   ├── p1.py
│   ├── p2.py
│   ├── p3.py
│   ├── p4.py
│   └── q
│       ├── __pycache__
│       │   └── p4.cpython-38.pyc
│       └── p4.py
└── t
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-38.pyc
    │   ├── c3.cpython-38.pyc
    │   └── c5.cpython-38.pyc
    ├── c3.py
    └── c5.py

p/p4.py

'''
    this is a doc
    hello world
'''

'''
    another doc
'''
print("name:"+__name__)
print("package:"+__package__)
print("file:"+__file__)
print("doc:"+__doc__)

c4.py

import p.p4
name:p.p4
package:p
file:/Users/scottxiong/Desktop/end/python/p/p4.py
doc:
    this is a doc
    hello world

将p4.py复制一份放入q下,改写c4.py

import p.q.p4
name:p.p4
package:p
file:/Users/scottxiong/Desktop/end/python/p/p4.py
doc:
    this is a doc
    hello world

results matching ""

    No results matching ""