Ejemplo n.º 1
0
#!/usr/bin/python
#coding:utf-8

import module1
from module1 import printer
from module2 import *
module1.printer('Use import')
printer('Use form import')
printer2('Use form import *')


import import_multiple
print import_multiple.spam
import_multiple.spam = 33
import import_multiple # the old one, won't load again
print import_multiple.spam
reload(import_multiple) # reload
print import_multiple.spam


s = """
- 模块语句会在首次导入时候执行
- 顶层的赋值语句会创建模块属性.
   * 不在 def和class之内的赋值语句,例如 =和def *
- 模块的命名空间能通过属性__dict__ 或者 dir(M) 获取
- 模块是一个独立的作用域(本地变量就是全局变量)

- reload 会在模块当前命名空间内执行模块文件的新建
- 文件中顶层赋值语句会使变量化成新值
- 重载会印象所有使用import读取了模块的客户端
- 重载只会对以后使用from的客户端造成影响
Ejemplo n.º 2
0
### file: module1.py

def printer(x):                   # Module attribute
    print(x)



>>> import module1                         # Get module as a whole
>>> module1.printer('Hello world!')        # Qualify to get names
Hello world!



>>> from module1 import printer            # Copy out one variable
>>> printer('Hello world!')                # No need to qualify name
Hello world!



>>> from module1 import *                   # Copy out all variables
>>> printer('Hello world!')
Hello world!



### file: simple.py

print('hello')
spam = 1                   # Initialize variable

Ejemplo n.º 3
0
import module1

module1.printer('Hi!')
print(module1)
# printer
print(module1.printer)
Ejemplo n.º 4
0
import module1  # Get module as a whole (one or more)
module1.printer('Hello world!')  # Qualify to get names - Hello world!
Ejemplo n.º 5
0
# The import statement

import module1  # Get module as a whole (one or more)
module1.printer('Hello world!')  # Qualify to get names

# The from Statement

from module1 import printer  # Copy out a variable (one or more)
printer('Hello world!')  # No need to qualify name

# The from * Statement

from module1 import *  # Copy out _all_variables
printer('Hello world!')

# Imports Happen Only Once

import simple  # First import: loads and runs file's code
print(simple.spam)  # Assignment makes an attribute
simple.spam = 2  # Change attribute in module
import simple  # Just fetches already loaded module
printer(simple.spam)  # Code wasn't rerun: attribute unchanged

# import and from Are Assignments

from small import x, y  # Copy two names out
x = 42  # Changes local x only
y[0] = 42  # Changes shared mutable in place

import small  # Get module name (from doesn't)
print(small.x)  # Smalls x is not my x
Ejemplo n.º 6
0
__author__ = 'pavang'
__Date___ = ''

import module1
module1.printer("Python")



module1.printer = "Hello"
print(module1.printer)

# print(module1.__dict__)

var = "var of init"


# accessing local and module attribute
print(var)
print(module1.var)
Ejemplo n.º 7
0
if __name__ == '__main__':
    from module1 import printer
    printer('hello world')
Ejemplo n.º 8
0
#!/usr/bin/env python3

import module1

module1.printer('Hello World!')
Ejemplo n.º 9
0
if __name__ == '__main__':
    import module1
    module1.printer('hello world')
# Python modules are easy to create; they’re just files of Python program code created
# with a text editor. You don’t need to write special syntax to tell Python you’re making
# a module; almost any text file will do. Because Python handles all the details of finding
# and loading modules, modules are also easy to use; clients simply import a module, or
# specific names a module defines, and use the objects they reference.

# To define a module, simply use your text editor to type some Python code into a text
# file, and save it with a “.py” extension; any such file is automatically considered a
# Python module

from module1 import printer
import module1

module1.printer('test but longer')
printer('test')

# When a module is imported, Python maps the internal module name to an external
# filename by adding a directory path from the module search path to the front, and
# a .py or other extension at the end. For instance, a module named M ultimately maps
# to some external file <directory>\M.<extension> that contains the module’s code.

# As mentioned in the preceding chapter, it is also possible to create a Python module by
# writing code in an external language such as C, C++, and others (e.g., Java, in the
# Jython implementation of the language). Such modules are called extension modules,
# and they are generally used to wrap up external libraries for use in Python scripts.

# import fetches the module as a whole, so you
# must qualify to fetch its names; in contrast, from fetches (or copies) specific names out
# of the module.

test = module1.printer
#!/usr/bin/env python3
# -*- coding=utf-8 -*-
# 本脚本由黄猛编写,用于Python学习!
import module1

module1.printer('欢迎来到乾颐堂!!')

if __name__ == '__main__':
    pass
Ejemplo n.º 12
0
#!/usr/bin/env python

print "------ starting OOP -------"

import sys

#from
#reload

import module1

module1.printer("test module")

from module1 import printer

printer("Using from", 123)
Ejemplo n.º 13
0
from module1 import printer  # Copy out a variable (one or more)
printer('Hello world!!')  # No need to qualify name - Hello world!!
Ejemplo n.º 14
0
#!/usr/bin/env python3
# -*- coding=utf-8 -*-
# 本脚由亁颐堂现任明教教主编写,用于乾颐盾Python课程!
# 教主QQ:605658506
# 亁颐堂官网www.qytang.com
# 教主技术进化论拓展你的技术新边疆
# https://ke.qq.com/course/271956?tuin=24199d8a

import module1
module1.printer('欢迎来到亁颐堂')
Ejemplo n.º 15
0
# import문

import module1                  # 모듈 전체를 가져옴(하나 또는 그 이상)
module1.printer('Hello world!') # 이름들을 가져오기 위해 인정(qualify)
# Hello world!
Ejemplo n.º 16
0
import module1

module1.printer("Hello World")

from module1 import printer2
printer2("Hello Again World")

import module2

print(module2.name)
print(module2.func)
print(module2.klass)
Ejemplo n.º 17
0
#!/usr/bin/env python3

import module1

module1.printer('')

from module1 import printer
printer('Hello world!')

from module1 import *
printer('Hello world!')

print('module1.spam =', module1.spam)
module1.spam = 2

import module1
print('module1.spam =', module1.spam)

from small import x, y
print("x: ", x)
print("y: ", y)

x = 42
y[0] = 42

import small
print("x: ", small.x)
print("y: ", small.y)

Ejemplo n.º 18
0
from module1 import printer
from module2 import printer

# module2's printer, it will override module1
printer("helllo")
Ejemplo n.º 19
0
#!/usr/bin/env python3
# -*- coding=utf-8 -*-
# 本脚由亁颐堂现任明教教主编写,用于乾颐盾Python课程!
# 教主QQ:605658506
# 亁颐堂官网www.qytang.com
# 教主技术进化论拓展你的技术新边疆
# https://ke.qq.com/course/271956?tuin=24199d8a

from module1 import printer

printer('欢迎来到亁颐堂')
Ejemplo n.º 20
0
# from문

from module1 import printer  # 변수를 복사(하나 또는 그 이상)
printer('Hello world!')  # 이름을 인정할 필요가 없음
# Hello world!
Ejemplo n.º 21
0
import module1
print(dir(module1))
module1.printer('taosm')
Ejemplo n.º 22
0
__author__ = 'pavang'
__Date___ = ''

import module1
# from module1 import printer
# from module1 import *


# print(module1.var)
module1.printer("Only Import")

print(module1.var)
# printer("From statement")

print('=' * 30 + '   module1 dir   ' + '=' * 40)

#print(dir(module1))

#includes inherited names for classes
# print(module1.__dict__)


# Another way of modifying attributes
module1.__dict__['var'] = 50
print(module1.var)
Ejemplo n.º 23
0
#!/usr/bin/env python3
# -*- coding=utf-8 -*-
# 本脚本由黄猛编写,用于Python学习!
from module1 import printer
printer('Welcome to the home of Zijie Huang!!!!')

if __name__ == '__main__':
    pass