面向对象-基础
面向对象
面向对象是一种抽象化的编程思想,很多编程语言中都有的一种思想
面向对象就是将编程当成是一个事物,对外界来说,事物是直接使用的,不用去管他内部的情况,而编程就是设置事物能够做什么事
面向对象三大特性
封装
将属性和⽅法书写到类的⾥⾯的操作即为封装
封装可以为属性和⽅法添加私有权限
继承
⼦类默认继承⽗类的所有属性和⽅法
⼦类可以重写⽗类属性和⽅法
多态
传⼊不同的对象,产⽣不同的结果
类和对象
在面向对象编程过程中,有两个重要组成部分:类和对象
类和对象的关系:用类去创建一个对象
类
类是对⼀系列具有相同特征和⾏为的事物的统称,是⼀个抽象的概念,不是真实存在的事物。
特征即是属性
⾏为即是⽅法
类是用来创建对象的
对象
对象事类创建出来的真实存在的事物,在开发中,先有类,再有对象
面向对象实现方法
定义类
类名要满足标识符命名规范,同时遵循大驼峰命名习惯
定义类语法:
class 类名():
代码
......
class Cooker(object):
def cook(self):
print("我会做饭")
创建对象
对象又名实例,语法:
对象名 = 类名()
# 定义类
class Cooker(object):
def cook(self):
print("我会做饭")
# 创建对象
cooker1 = Cooker()
print(cooker1)
# cooker1 对象调用实例方法
cooker1.cook()
注意:创建对象的过程也叫实例化对象。
self
self指的是调⽤该函数的对象。
class Cooker(object):
def cook(self):
print("我会做饭")
print(self)
# 创建对象
cooker1 = Cooker()
print(cooker1)
# cooker1 对象调用实例方法
cooker1.cook()
cooker2 = Cooker()
print(cooker2)
注意:打印对象和self得到的结果是⼀致的,都是当前对象的内存中存储的地址。
添加和获取对象属性
类外面添加对象属性
语法:
对象名.属性名 = 值
cooker1.name = "王五"
cooker1.age = 32
类外面获取对象属性
语法:
对象名.属性名
print(f"厨师cooker1的名字是{cooker1.name}")
print(f"厨师cooker1的年龄是{cooker1.age}")
类里面获取对象属性
语法:
self.属性名
class Cooker(object):
def print_info(self):
# 类里面获取实例属性
print(f"厨师cooker1的名字是{cooker1.name}")
print(f"厨师cooker1的年龄是{cooker1.age}岁")
cooker1 = Cooker()
# 添加实例属性
cooker1.name = "王五"
cooker1.age = 32
cooker1.print_info()
魔法方法
__init()__方法的作用:初始化对象
注意:
__init__() ⽅法,在创建⼀个对象时默认被调⽤,不需要⼿动调⽤
__init__(self) 中的self参数,不需要开发者传递,python解释器会⾃动把当前的对象引
⽤传递过去。
class Cooker(object):
# 定义初始化功能的函数
def __init__(self):
# 添加实例属性
self.name = "王五"
self.age = 32
def print_info(self):
# 类里面调用实例对象
print(f"厨师的姓名是 {self.name}, 年龄是{self.age}岁")
cooker1 = Cooker()
cooker1.print_info()
带参数的__init__():一个类可以创建多个对象,多个对象有不同的初始化属性
class Cooker(object):
# 定义初始化功能的函数
def __init__(self, name, age):
# 添加实例属性
self.name = name
self.age = age
def print_info(self):
# 类里面调用实例对象
print(f"厨师的姓名是 {self.name}, 年龄是{self.age}岁")
cooker1 = Cooker("王五", 32)
cooker1.print_info()
cooker2 = Cooker("张三", 28)
cooker2.print_info()
__str__()
当使⽤print输出对象的时候,默认打印对象的内存地址。如果类定义了 __str__ ⽅法,那么就会打印从在这个⽅法中 return 的数据。
class Cooker(object):
# 定义初始化功能的函数
def __init__(self, name, age):
# 添加实例属性
self.name = name
self.age = age
def print_info(self):
# 类里面调用实例对象
print(f"厨师的姓名是 {self.name}, 年龄是{self.age}岁")
def __str__(self):
return f"这是厨师{self.name}的简介"
cooker1 = Cooker("王五", 32)
print(cooker1)
__del__()
当删除对象时,python解释器也会默认调用__del__()方法
class Cooker(object):
# 定义初始化功能的函数
def __init__(self, name, age):
# 添加实例属性
self.name = name
self.age = age
def print_info(self):
# 类里面调用实例对象
print(f"厨师的姓名是 {self.name}, 年龄是{self.age}岁")
def __del__(self):
print(f"{self} 对象已经被删除")
cooker1 = Cooker("王五", 32)
print(cooker1)
面向对象-继承
在Python中,所有类默认继承object类,object类是顶级类或基类;其他⼦类叫做派⽣类。
Python⾯向对象的继承指的是多个类之间的所属关系,即⼦类默认继承⽗类的所有属性和⽅法,具体如下:
# ⽗类A
class A(object):
def __init__(self):
self.num = 1
def info_print(self):
print(self.num)
# ⼦类B
class B(A):
pass
result = B()
result.info_print() # 1
单继承
子类只继承一个父类
# 1. 师⽗类
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 2. 徒弟类
class Prentice(Master):
pass
# 3. 创建对象daqiu
daqiu = Prentice()
# 4. 对象访问实例属性
print(daqiu.kongfu)
# 5. 对象调⽤实例⽅法
daqiu.make_cake()
多继承
子类同时继承多个父类
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(object):
def __init__(self):
self.kongfu = '[米其林三星主厨煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
class Prentice(School, Master):
pass
daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()
注意:当⼀个类有多个⽗类的时候,默认使⽤第⼀个⽗类的同名属性和⽅法。
子类重写父类同名方法和属性
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(object):
def __init__(self):
self.kongfu = '[米其林三星主厨煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()
print(Prentice.__mro__)
子类和父类具有同名属性和方法,默认使用子类的同名属性和方法
子类调用父类的同名方法和属性
先调用子类方法,再调用父类方法
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(object):
def __init__(self):
self.kongfu = '[米其林三星主厨煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果⼦配⽅]'
def make_cake(self):
# 如果是先调⽤了⽗类的属性和⽅法,⽗类属性会覆盖⼦类属性,故在调⽤属性前,先调⽤⾃⼰⼦类的初始化
self.__init__()
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 调⽤⽗类⽅法,但是为保证调⽤到的也是⽗类的属性,必须在调⽤⽅法前调⽤⽗类的初始化
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
daqiu = Prentice()
daqiu.make_cake()
daqiu.make_master_cake()
daqiu.make_school_cake()
daqiu.make_cake()
多层继承
子类继承的类也是另外父类的子类
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(object):
def __init__(self):
self.kongfu = '[米其林三星主厨煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果⼦配⽅]'
def make_cake(self):
# 如果是先调⽤了⽗类的属性和⽅法,⽗类属性会覆盖⼦类属性,故在调⽤属性前,先调⽤⾃⼰⼦类的初始化
self.__init__()
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 调⽤⽗类⽅法,但是为保证调⽤到的也是⽗类的属性,必须在调⽤⽅法前调⽤⽗类的初始化
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
# 徒孙类
class Tusun(Prentice):
pass
xiaoqiu = Tusun()
xiaoqiu.make_cake()
xiaoqiu.make_school_cake()
xiaoqiu.make_master_cake()
super()调用父类方法
注意:使⽤super() 可以⾃动查找⽗类。调⽤顺序遵循 __mro__ 类属性的顺序。⽐较适合单继承使⽤。
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(Master):
def __init__(self):
self.kongfu = '[米其林三星主厨煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# ⽅法2.1
# super(School, self).__init__()
# super(School, self).make_cake()
# ⽅法2.2
super().__init__()
super().make_cake()
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果⼦配⽅]'
def make_cake(self):
# 如果是先调⽤了⽗类的属性和⽅法,⽗类属性会覆盖⼦类属性,故在调⽤属性前,先调⽤⾃⼰⼦类的初始化
self.__init__()
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# ⼦类调⽤⽗类的同名⽅法和属性:把⽗类的同名属性和⽅法再次封装
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
# 一次性调用父类的同名属性和方法
def make_old_cake(self):
# ⽅法⼀:代码冗余;⽗类类名如果变化,这⾥代码需要频繁修改
# Master.__init__(self)
# Master.make_cake(self)
# School.__init__(self)
# School.make_cake(self)
# ⽅法⼆: super()
# ⽅法2.1 super(当前类名, self).函数()
# super(Prentice, self).__init__()
# super(Prentice, self).make_cake()
# ⽅法2.2 super().函数()
super().__init__()
super().make_cake()
daqiu = Prentice()
daqiu.make_cake()
定义私有属性和方法
在Python中,可以为实例属性和⽅法设置私有权限,即设置某个实例属性或实例⽅法不继承给⼦类。
设置私有权限的⽅法:在属性名和⽅法名 前⾯ 加上两个下划线 __。
注意:私有属性和私有⽅法只能在类⾥⾯访问和修改。
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(object):
def __init__(self):
self.kongfu = '[米其林三星主厨煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果⼦配⽅]'
# 定义私有属性
self.__money = 2000000
# 定义私有方法
def __info_print(self):
print(self.kongfu)
print(self.__money)
def make_cake(self):
# 如果是先调⽤了⽗类的属性和⽅法,⽗类属性会覆盖⼦类属性,故在调⽤属性前,先调⽤⾃⼰⼦类的初始化
self.__init__()
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# ⼦类调⽤⽗类的同名⽅法和属性:把⽗类的同名属性和⽅法再次封装
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
# 徒孙类
class Tusun(Prentice):
pass
daqiu = Prentice()
# 对象不能访问私有属性和私有⽅法
# print(daqiu.__money)
# daqiu.__info_print()
xiaoqiu = Tusun()
# ⼦类⽆法继承⽗类的私有属性和私有⽅法
# print(xiaoqiu.__money) # ⽆法访问实例属性__money
# xiaoqiu.__info_print()
获取和修改私有属性值
在Python中,⼀般定义函数名 get_xx ⽤来获取私有属性,定义 set_xx ⽤来修改私有属性值。
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# 创建学校类
class School(object):
def __init__(self):
self.kongfu = '[米其林三星主厨煎饼果⼦配⽅]'
def make_cake(self):
print(f'运⽤{self.kongfu}制作煎饼果⼦')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果⼦配⽅]'
# 定义私有属性
self.__money = 2000000
# 获取私有属性方法
def get_money(self):
return self.__money
# 修改私有属性方法
def set_money(self, num):
self.__money = num
# 定义私有方法
def __info_print(self):
print(self.kongfu)
print(self.__money)
def make_cake(self):
# 如果是先调⽤了⽗类的属性和⽅法,⽗类属性会覆盖⼦类属性,故在调⽤属性前,先调⽤⾃⼰⼦类的初始化
self.__init__()
print(f'运⽤{self.kongfu}制作煎饼果⼦')
# ⼦类调⽤⽗类的同名⽅法和属性:把⽗类的同名属性和⽅法再次封装
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake(self)
# 徒孙类
class Tusun(Prentice):
pass
daqiu = Prentice()
xiaoqiu = Tusun()
# 调用get_money函数获取私有属性money的值
print(xiaoqiu.get_money())
# 调用set_money函数修改私有属性money的值
xiaoqiu.set_money(4587)
print(xiaoqiu.get_money())
面向对象-多态
多态是什么
多态指的是⼀类事物有多种形态,(⼀个抽象类有多个⼦类,因⽽多态的概念依赖于继承)。
定义:多态是⼀种使⽤对象的⽅式,⼦类重写⽗类⽅法,调⽤不同⼦类对象的相同⽗类⽅法,可以产⽣不同的执⾏结果
好处:调⽤灵活,有了多态,更容易编写出通⽤的代码,做出通⽤的编程,以适应需求的不断变化!
实现步骤:
定义⽗类,并提供公共⽅法
定义⼦类,并重写⽗类⽅法
传递⼦类对象给调⽤者,可以看到不同⼦类执⾏效果不同
多态示例代码
class Dog(object):
def work(self): # ⽗类提供统⼀的⽅法,哪怕是空⽅法
print("指哪打哪儿")
class ArmyDog(Dog): # 继承Dog类
def work(self): # ⼦类重写⽗类同名⽅法
print('追击敌⼈...')
class DrugDog(Dog):
def work(self):
print('追查毒品...')
class Person(object):
# 传⼊不同的对象,执⾏不同的代码,即不同的work函数
def work_with_dog(self, dog):
dog.work()
ad = ArmyDog()
dd = DrugDog()
daqiu = Person()
daqiu.work_with_dog(ad)
daqiu.work_with_dog(dd)
类属性和实例方法
类属性
设置和访问类属性
类属性就是 类对象 所拥有的属性,它被 该类的所有实例对象 所共有。
类属性可以使⽤ 类对象 或 实例对象 访问。
类属性的优点
类的实例 记录的某项数据 始终保持⼀致时,则定义类属性。
实例属性 要求 每个对象 为其 单独开辟⼀份内存空间 来记录数据,⽽ 类属性 为全类所共有,仅占⽤⼀份内存,更加节省内存空间。
class Dog(object):
tooth = 10
wangcai = Dog()
xiaohei = Dog()
print(Dog.tooth) # 10
print(wangcai.tooth) # 10
print(xiaohei.tooth) # 10
修改类属性
类属性只能通过类对象修改,不能通过实例对象修改,如果通过实例对象修改类属性,表示的是创建了⼀个实例属性。
class Dog(object):
tooth = 10
wangcai = Dog()
xiaohei = Dog()
# 修改类属性
Dog.tooth = 12
print(Dog.tooth) # 12
print(wangcai.tooth) # 12
print(xiaohei.tooth) # 12
# 不能通过对象修改属性,如果这样操作,实则是创建了⼀个实例属性
wangcai.tooth = 20
print(Dog.tooth) # 12
print(wangcai.tooth) # 20
print(xiaohei.tooth) # 12
实例属性
class Dog(object):
def __init__(self):
self.age = 5
def info_print(self):
print(self.age)
wangcai = Dog()
print(wangcai.age) # 5
# print(Dog.age) # 报错:实例属性不能通过类访问
wangcai.info_print() # 5
类方法和静态方法
类方法
类方法的特点:
第⼀个形参是类对象的⽅法
需要⽤装饰器 @classmethod 来标识其为类⽅法,对于类⽅法,第⼀个参数必须是类对象,⼀般以cls 作为第⼀个参数。
类方法的使用场景
当⽅法中 需要使⽤类对象 (如访问私有类属性等)时,定义类⽅法
类⽅法⼀般和类属性配合使⽤
class Dog(object):
__tooth = 10
@classmethod
def get_tooth(cls):
return cls.__tooth
wangcai = Dog()
result = wangcai.get_tooth()
print(result) # 10
静态方法
静态方法的特点
需要通过装饰器 @staticmethod 来进⾏修饰,静态⽅法既不需要传递类对象也不需要传递实例对象(形参没有self/cls)。
静态⽅法 也能够通过 实例对象 和 类对象 去访问。
静态方法的使用场景
当⽅法中 既不需要使⽤实例对象(如实例对象,实例属性),也不需要使⽤类对象 (如类属性、类⽅法、创建实例等)时,定义静态⽅法
取消不需要的参数传递,有利于 减少不必要的内存占⽤和性能消耗
class Dog(object):
@staticmethod
def info_print():
print('这是⼀个狗类,⽤于创建狗实例....')
wangcai = Dog()
# 静态⽅法既可以使⽤对象访问⼜可以使⽤类访问
wangcai.info_print()
Dog.info_print()
异常
当检测到⼀个错误时,解释器就⽆法继续执⾏了,反⽽出现了⼀些错误的提示,这就是所谓的"异常"。
例如:以 r ⽅式打开⼀个不存在的⽂件。
open('test.txt', 'r')
# 将会有一下报错,程序将不会继续执行
# Traceback (most recent call last):
# File "E:\Web\0.python基础\test.py", line 1, in <module>
# open('test.txt', 'r')
# FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
异常的写法
语法:
try:
可能发⽣错误的代码
except:
如果出现异常执⾏的代码
# 尝试以 r 模式打开⽂件,如果⽂件不存在,则以 w ⽅式打开。
try:
f = open('test.txt', 'r')
except:
f = open('test.txt', 'w')
捕获指定异常
注意:
如果尝试执⾏的代码的异常类型和要捕获的异常类型不⼀致,则⽆法捕获异常。
⼀般try下⽅只放⼀⾏尝试执⾏的代码。
语法:
try:
可能发⽣错误的代码
except 异常类型:
如果捕获到该异常类型执⾏的代码
try:
print(num)
except NameError:
print('有错误')
捕获多个指定异常
当捕获多个异常时,可以把要捕获的异常类型的名字,放到except 后,并使⽤元组的⽅式进⾏书写。
try:
print(1 / 0)
except (NameError, ZeroDivisionError):
print('有错误')
捕获异常描述信息
try:
print(num)
except (NameError, ZeroDivisionError) as result:
print(result)
捕获所有异常
Exception是所有程序异常类的⽗类。
try:
print(num)
except Exception as result:
print(result)
异常的else
else表示的是如果没有异常要执⾏的代码。
try:
print(1)
except Exception as result:
print(result)
else:
print('我是else,是没有异常的时候执⾏的代码')
异常的finally
finally表示的是⽆论是否异常都要执⾏的代码,例如关闭⽂件。
try:
f = open('test.txt', 'r')
except Exception as result:
f = open('test.txt', 'w')
else:
print('没有异常,真开⼼')
finally:
f.close()
异常的传递
尝试只读⽅式打开test.txt⽂件,如果⽂件存在则读取⽂件内容,⽂件不存在则提示⽤户即可。
读取内容要求:尝试循环读取内容,读取过程中如果检测到⽤户意外终⽌程序,则 except 捕获异常并提示⽤户。
import time
try:
f = open('test.txt')
try:
while True:
content = f.readline()
if len(content) == 0:
break
time.sleep(2)
print(content)
except:
# 如果在读取⽂件的过程中,产⽣了异常,那么就会捕获到
# ⽐如 按下了 ctrl+c
print('意外终⽌了读取数据')
finally:
f.close()
print('关闭⽂件')
except:
print("没有这个⽂件")
自定义异常
在Python中,抛出⾃定义异常的语法为 raise 异常类对象 。
密码⻓度不⾜,则报异常(⽤户输⼊密码,如果输⼊的⻓度不⾜3位,则报错,即抛出⾃定义异常,并捕获该异常)。
# ⾃定义异常类,继承Exception
class ShortInputError(Exception):
def __init__(self, length, min_len):
self.length = length
self.min_len = min_len
# 设置抛出异常的描述信息
def __str__(self):
return f'你输⼊的⻓度是{self.length}, 不能少于{self.min_len}个字符'
def main():
try:
con = input('请输⼊密码:')
if len(con) < 3:
raise ShortInputError(len(con), 3)
except Exception as result:
print(result)
else:
print('密码已经输⼊完成')
main()
模块-包
模块
Python 模块(Module),是⼀个 Python ⽂件,以 .py 结尾,包含了 Python 对象定义和Python语句。
模块能定义函数,类和变量,模块⾥也能包含可执⾏的代码。
导入模块的方式
import 模块名
from 模块名 import 功能名
from 模块名 import *
import 模块名 as 别名
from 模块名 import 功能名 as 别名
制作模块
在Python中,每个Python⽂件都可以作为⼀个模块,模块的名字就是⽂件的名字。也就是说⾃定义模块名必须要符合标识符命名规则。
定义模块:
新建⼀个Python⽂件,命名为 my_module1.py ,并定义 testA 函数。
def testA(a, b):
print(a + b)
测试模块:
在实际开中,当⼀个开发⼈员编写完⼀个模块后,为了让模块能够在项⽬中达到想要的效果,这个开发
⼈员会⾃⾏在py⽂件中添加⼀些测试信息.,例如,在 my_module1.py ⽂件中添加测试代码。
def testA(a, b):
print(a + b)
testA(1, 1)
此时,⽆论是当前⽂件,还是其他已经导⼊了该模块的⽂件,在运⾏的时候都会⾃动执⾏ testA 函数的调⽤。
解决办法如下:
def testA(a, b):
print(a + b)
# 只在当前⽂件中调⽤该函数,其他导⼊的⽂件内不符合该条件,则不执⾏testA函数调⽤
if __name__ == '__main__':
testA(1, 1)
如果使⽤ from .. import .. 或 from .. import * 导⼊多个模块的时候,且模块内有同名功能。当调
⽤这个同名功能的时候,调⽤到的是后⾯导⼊的模块的功能。
# 模块1代码
def my_test(a, b):
print(a + b)
# 模块2代码
def my_test(a, b):
print(a - b)
# 导⼊模块和调⽤功能代码
from my_module1 import my_test
from my_module2 import my_test
# my_test函数是模块2中的函数
my_test(1, 1)
模块定位顺序
当导⼊⼀个模块,Python解析器对模块位置的搜索顺序是:
当前⽬录
如果不在当前⽬录,Python则搜索在shell变量PYTHONPATH下的每个⽬录。
如果都找不到,Python会察看默认路径。UNIX下,默认路径⼀般为/usr/local/lib/python/
模块搜索路径存储在system模块的sys.path变量中。变量⾥包含当前⽬录,PYTHONPATH和由安装过程决定的默认⽬录。
注意:
⾃⼰的⽂件名不要和已有模块名重复,否则导致模块功能⽆法使⽤
使⽤from 模块名 import 功能 的时候,如果功能名字重复,调⽤到的是最后定义或导⼊的功 能。
__all__()
如果⼀个模块⽂件中有 __all__ 变量,当使⽤ from xxx import * 导⼊时,只能导⼊这个列表中的元素。
__all__ = ['testA']
def testA():
print('testA')
def testB():
print('testB')
from my_module1 import *
testA()
# testB将会报错: name 'testB' is not defined
testB()
包
包将有联系的模块组织在⼀起,即放到同⼀个⽂件夹下,并且在这个⽂件夹创建⼀个名字为 __init__.py ⽂件,那么这个⽂件夹就称之为包。
制作包
[New] — [Python Package] — 输⼊包名 — [OK] — 新建功能模块(有联系的模块)。
注意:新建包后,包内部会⾃动创建 __init__.py ⽂件,这个⽂件控制着包的导⼊⾏为。
示例
新建包 mypackage
新建包内模块: my_module1 和 my_module2
模块内代码如下
# my_module1
print(1)
def info_print1():
print('my_module1')
# my_module2
print(2)
def info_print2():
print('my_module2')
导入包
方法一
import 包名.模块名
包名.模块名.目标
import my_package.my_module1
my_package.my_module1.info_print1()
方法二:注意:必须在 __init__.py ⽂件中添加 __all__ = [] ,控制允许导⼊的模块列表。
from 包名 import *
模块名.⽬标
from my_package import *
my_module1.info_print1()