コード例 #1
0
A module is typically any python file that contains only functions and classes. A good example of module you may have used before is the math module. We can create and import our own modules.

Below is the code contained in two separate python files.

# File name: myModule.py

def func(x):
    return x + 2

def func2(y):
    return y * 78
# File name: main.py

import myModule

value = myModule.func(5)  # this will call the func function from myModule

print(value)  # this prints 7
We can use the import keyword to import modules that are built into python or that we've created.
Note: To import our own modules they must be in the same directory as the file we are importing from.

To avoid the use of "myModule" we can import specific functions from our modules using a combination of keywords "import" and "from".

# File name: main.py

from myModule import func

value = func(5)  # now we can just use func like a regular function

print(value)  # this prints 7
コード例 #2
0
#!/usr/bin/python
# -*- coding: UTF-8 -*-

#调用自定义模块的类和函数
import myModule

myModule.func()
myClass = myModule.MyClass()
myClass.myFunc()
コード例 #3
0
ファイル: import.py プロジェクト: mingming2513953126/Demo
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import myModule                         # 导入 myModule 模块
print "count =", myModule.func()        # 调用函数func()
myModule.count = 10                     # 给模块myModule中变量count赋值,此时count=10
print "count =", myModule.count         # 获取变量count的值   输出:10

import myModule                         # 再次导入 myModule 模块     变量count的初始值为10
print "count =", myModule.func()        # 调用func(),变量count的值加1. 输出结果:count=11

# import置于条件语句中
if myModule.count > 1:                  # 判断myModule.count的值是否大于1
    myModule.count = 1                  # 如果count的值大于1,则把count变成1.  之前:11   所以变成1
else:
    import myModule                     # 如果count的值小于1,则把count变成myModule模块
print "count =", myModule.count         # 输出:1
コード例 #4
0
ファイル: import.py プロジェクト: ipandage/language_study
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import myModule
print "count =", myModule.func()
myModule.count = 10
print "count =", myModule.count

import myModule
print "count =", myModule.func()

# import�������������
if myModule.count > 1:
    myModule.count = 1
else:
    import myModule
print "count =", myModule.count
コード例 #5
0
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 调用自定义模块的类和函数
import myModule

myModule.func(
)  # 调用模块的函数       调用时需要加前缀,否则python不知道func()所在模块的命名空间。输出:MyModule.func()
myClass = myModule.MyClass()  # 创建类Myclass的实例myclass。这里也需要使用myModule来调用类
myClass.myFunc()  # 调用类的方法myFunction()    输出结果MyModule.MyClass.myFunc()
コード例 #6
0
import myModule

print("count =", myModule.func())
myModule.count = 10
print("count =", myModule.count)
import myModule
print("count =", myModule.func())
コード例 #7
0
ファイル: import.py プロジェクト: ipandage/python-study
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import myModule        
print "count =", myModule.func()           
myModule.count = 10
print "count =", myModule.count

import myModule 
print "count =", myModule.func() 

# import�������������
if myModule.count > 1:
    myModule.count = 1    
else:
    import myModule
print "count =", myModule.count