随着不断给类增加新的功能,文件可能变得很长。为使文件简洁,python允许你将类存储在模块中,然后在主程序中导入所需要的模块
格式:from 模板(存储类的文件) in 类名
1、导入单个类
在模板dog.py中包含Dog类的源代码。在my_dog.py文件中导入Dog类并创建其实例。
dog.py源码:
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self, name, age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗被命令时蹲下"""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
my_dog.py源码:
from dog import Dog
my_dog = BigDog('willie', 6)
print("My dog's name is "+my_dog.name.title()+".")
print("My dog is " + str(my_dog.age)+" years old.
")
my_dog.sit()
my_dog.roll_over()
结果:
2、在一个模板中存储多个类
在模板dog.py中包含多个类的源代码。在my_dog.py文件中导入BigDog类并创建其实例。
dog.py源代码:
class Mao():
def __init__(self, color="yello")
self.color = colors
def describe_color(self):
print("The color is "+self.color)
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self, name, age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗被命令时蹲下"""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
class BigDog(Dog):
"""big dog's special"""
def __init__(self, name, age):
super().__init__(name, age)
self.length = 100
self.mao = Mao()
def describe_length(self):
print("The BigDog's length is "+str(self.length)+" km")
my_dog.py源代码:
my_dog = BigDog('willie', 6)
print("My dog's name is "+my_dog.name.title()+".")
print("My dog is " + str(my_dog.age)+" years old.
")
my_dog.sit()
my_dog.roll_over()
my_dog.describe_length()
my_dog.mao.describe_color()
结果:
3、从一个模板中导入多个类
格式:from 模板 import 类名1,类名2,,,
示例代码:
dog.py源代码:
class Mao():
def __init__(self, color="yello")
self.color = colors
def describe_color(self):
print("The color is "+self.color)
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self, name, age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗被命令时蹲下"""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
class BigDog(Dog):
"""big dog's special"""
def __init__(self, name, age):
super().__init__(name, age)
self.length = 100
self.mao = Mao()
def describe_length(self):
print("The BigDog's length is "+str(self.length)+" km")
my_dog.py源代码:
from dog import Mao, Dog, BigDog
mydog1 = dog.Dog("haha", 7)
mydog1.sit()
mydog1.roll_over()
print("
")
my_dog = dog.BigDog('willie', 6)
print("My dog's name is "+my_dog.name.title()+".")
print("My dog is " + str(my_dog.age)+" years old.
")
my_dog.sit()
my_dog.roll_over()
my_dog.describe_length()
my_dog.mao.describe_color()
结果:
4、导入整个模板
格式:import 模板
注意:在使用类创建实例的时候要用“模板名.类名"
示例代码:
dog.py源代码:
class Mao():
def __init__(self, color="yello")
self.color = colors
def describe_color(self):
print("The color is "+self.color)
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self, name, age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗被命令时蹲下"""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
class BigDog(Dog):
"""big dog's special"""
def __init__(self, name, age):
super().__init__(name, age)
self.length = 100
self.mao = Mao()
def describe_length(self):
print("The BigDog's length is "+str(self.length)+" km")
my_dog.py源代码:
import dog
mydog1 = dog.Dog("haha", 7)
mydog1.sit()
mydog1.roll_over()
print("
")
my_dog = dog.BigDog('willie', 6)
print("My dog's name is "+my_dog.name.title()+".")
print("My dog is " + str(my_dog.age)+" years old.
")
my_dog.sit()
my_dog.roll_over()
my_dog.describe_length()
my_dog.mao.describe_color()
结果:
5、导入模板中的所有类
格式:from 模板 import *
代码及执行结果同上
6、在一个模板中导入另一个模板
格式:from 模板 import 类名
7、Python标准库
python标准库是一组模板,安装的python都包含它。
使用方法:
from 模板(文件名字)import 类名