思维导图
- 包、模块管理
- 私有的本质
- 面向对象的三大特性
- 多继承的顺序问题(不提议多继承)
- 继承 中的方法重写:两种方式 覆盖 和扩展(super) 其实没多大区别
- 类的属性、类方法、静态方法 实例属性和实例方法
- 单例
- 异常 try except else finally
- 文件
- eval函数
code
- objects项目
面向对象
-
内置函数 使用函数
dir()
可查看所有的内置变量 -
举例说明 注意:类名和文件名不能一致 否则会报错
calss 类名:
def func1(self, arg1...):
pass
-
方法的定义格式几乎和学过的函数一样 区别在于 第一个参数必须是 self
-
创建对象
对象变量 = 类名()
-
在init中构造的类变量会被pycharm记住, 在使用类对象的时候会有提示,在普通方法中构造的变量也会有提示,但是在类对象调用这个方法之前使用这个变量会报错 因为这个变量还没有固赋值(此处所说的变量是公开的,不是私有的)
-
python中的私有并非真正的私有, 只是将这个变量或者方法的名字改掉了 比如我有一个student类,类中有一个__age私有变量, 那么 在外部使用
stu._student__age
就可以访问到__age这个变量 私有方法亦是如此 -
python继承中,不会自动调用父类的构造函数必须手动调用
def __init__(self):
print("this is __init__ function in subbird")
super(subbird, self).__init__() # 也可以是 super().__init__()
class cat(animal):
def __init__(self):
super().__init__()
print("__init__ in cat class")
- 继承的方法也是
def eat(self):
print("eat in cat")
super().eat()
- 在子类中 即使使用 super()._animal__age 也不能访问到父类的私有成员
- python中的类属性和对象属性
- 类属性是指 在类中定义的属性
- 对象属性是指在构造方法中定义的属性
- 调用类属性可以使用 类.属性 也可以使用对象.属性 但是推荐使用类.属性
- 注意:当给类属性赋值的时候,不能使用对象.属性 = val 否则会在对象中增加一个属性 而不是给类属性赋值
class subbird(bird2, bird):
count = 0
__num = 0
def __init__(self):
subbird.count += 1
subbird.__num += 1
print("this is __init__ function in subbird")
super(subbird, self).__init__()
def bark(self):
print("bark in subbird")
super(subbird, self).bark()
sub1 = subbird()
print(subbird.count)
print(subbird._subbird__num)
sub1.count = 100 #
print(sub1.count) # 100
print(subbird.count) # 2
- 类方法可以调用类属性和其他的类方法
- 类方法的定义
@classmethod
def method1(cls):
pass
- 类方法的调用:
类名.类方法 类对象.类方法
静态方法
-
应用场景
- 既 不需要 访问 实例属性 或者调用 实例方法
- 也 不需要 访问 类属性 或者调用 类方法
-
定义
@staticmethod
def method2():
print("this is static method in people class")
pass
- 调用
类名.静态方法
对象名.静态方法
单例模式
new 方法
- 是object的内置静态方法
- 作用:
- 在内存中为对象分配空间
- 返回对象的引用
- Python 的解释器获得对象的 引用 后,将引用作为 第一个参数,传递给 init 方法
- 重写方法:
def __new__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super().__new__(cls)
return cls.instance
举例说明:
class singlepeople:
instance = None
init_flag = False
def __init__(self):
if not singlepeople.init_flag:
singlepeople.init_flag = True
else:
return
self.name = None
pass
def __new__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super().__new__(cls)
return cls.instance
---
from models.singlepeople import singlepeople
p = singlepeople()
print(p)
p.name = "smx"
print(p.name)
p2 = singlepeople()
# p2.name = "xyj"
print(p2)
print(p2.name)
print(p.name)
异常
目标
- 异常的概念
- 捕获异常
- 传递异常
- 抛出异常
捕获异常
- 简单的捕获异常语法
try:
# 提示用户输入一个数字
num = int(input("请输入数字:"))
except:
print("请输入正确的数字")
- 错误类型捕获
- 语法
try:
# 尝试执行的代码
pass
except 错误类型1:
# 针对错误类型1,对应的代码处理
pass
except 错误类型2:
# 针对错误类型2,对应的代码处理
pass
except (错误类型3, 错误类型4):
# 针对错误类型3 和 4,对应的代码处理
pass
except Exception as result:
# 打印错误信息
print(result)
else:
# 没有异常才会执行的代码
pass
finally:
# 无论是否有异常,都会执行的代码
print("无论是否有异常,都会执行的代码")
- 举例:
try:
num = int(input("请输入整数:"))
result = 8 / num
print(result)
except ValueError:
print("请输入正确的整数")
except ZeroDivisionError:
print("除 0 错误")
except Exception as result:
print("未知错误 %s" % result)
else:
print("正常执行")
finally:
print("执行完成,但是不保证正确")
抛出异常
- 代码
# 1> 创建异常对象 - 使用异常的错误信息字符串作为参数
ex = Exception("密码长度不够")
# 2> 抛出异常对象
raise ex
模块和包
目标
- 模块
- 包
- 发布模块
模块
- 模块是 Python 程序架构的一个核心概念
- 每一个以扩展名 py 结尾的 Python 源代码文件都是一个 模块
- 模块名 同样也是一个 标识符,需要符合标识符的命名规则
- 在模块中定义的 全局变量 、函数、类 都是提供给外界直接使用的 工具
- 模块 就好比是 工具包,要想使用这个工具包中的工具,就需要先 导入 这个模块
导入模块
- import 导入
import module
import module as md
- from import 导入
from module import *
-
模块搜索顺序
图片.png
-
原则
图片.png
包

发布模块



文件
目标
- 文件
- 文件操作
- 文件、文件夹的常用操作
- 文本文件的编码方式
文件读写






文件目录常用操作

文件的编码格式


eval函数

教程笔记图

-
初始化方法
image.png
image.png
image.png
-
图片.png
图片.png
网友评论