
1 metaclass 元类
1.1 创建类对象的类,即为元类
1.2 追查类对象的__class__指向的值
元类的对应的 class 就是 type
num = 10
print(num.__class__) # <class 'int'>
s = "abc"
print(s.__class__) # <class 'str'>
class Person:
pass
p = Person()
print(p.__class__) # <class '__main__.Person'>
print("-"*20)
print(int.__class__)
# print(num.__class__.__class__)
print(str.__class__)
print(Person.__class__)
print(Person.__class__.__class__)
print(type.__class__)
>>> 打印结果
<class 'int'>
<class 'str'>
<class '__main__.Person'>
--------------------
<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>
1.3 对象及类的结构图:

2 类对象的创建
2.1 通常使用 class 声明创建类对象(编译器工作)
class Person:
count = 0
def run(self):
pass
2.2 手动创建类对象
- 之前,我们使用过 type() 来查看一个对象的类型
type("abc")
- 通过 type() 方法手动创建类对象
def run(self):
print("---", self)
xxx = type("Dog",(),{"count": 0, "run": run})# Dog 是类名,xxx 是类变量;相对于前面, Person既是类名也是类变量
print(xxx)
print(xxx.__dict__)
d = xxx()
print(d)
d.run()
>>> 结果
<class '__main__.Dog'>
{'count': 0, 'run': <function run at 0x1054edbf8>, '__module__': '__main__', '__dict__': <attribute '__dict__' of 'Dog' objects>, '__weakref__': <attribute '__weakref__' of 'Dog' objects>, '__doc__': None}
<__main__.Dog object at 0x1054f0ac8>
--- <__main__.Dog object at 0x1054f0ac8>
3 类对象创建时,元类的查找机制
类的创建流程:
- 检测类中是否有明确 __metaclass__属性
有, 则通过指定元类来创建这个类对象
# 在类名后面括号内指定
class Dog(Animal):
pass
或
# 在类代码体内通过 \_\_metaclass\_\_ 属性进行指定
class Dog():
__metaclass__ = Animal
pass
- 检测父类中是否存在 __metaclass__属性
有, 则通过指定元类来创建这个类对象
class Animal:
__metaclass__ = xxx
pass
- 检测模块中是否存在 __metaclass__属性
有, 则通过指定元类来创建这个类对象
# 模块就是当前文件中有没有写上下面代码,注意,不能写在 类代码体内、函数体内等等
__metaclass__ = xxx
- 通过内置的type这个元类,来创建这个类对象
注意:动态变化类型时的拦截时机有3个(前三步)以及作用范围的不同
4 类的描述
class Person:
"""
关于这个类的描述, 类的作用, 类的构造函数等等; 类属性的描述
Attributes:
count: int 属性意义
"""
count = 1
def run(self, distance, step):
"""
这个方法的作用描述
:param distance: 参数的含义, 参数的类型int, 是否有默认值
:param step:
:return: 返回的结果的含义(时间), 返回数据的类型int
"""
print("人在跑")
return distance / step
# 通过 help 打印该类描述,pycharm 会自动生成一定的格式描述
help(Person)
>>> 结果
Help on class Person in module __main__:
class Person(builtins.object)
| 关于这个类的描述, 类的作用, 类的构造函数等等; 类属性的描述
| Attributes:
| count: int 属性意义
|
| Methods defined here:
|
| run(self, distance, step)
| 这个方法的作用描述
| :param distance: 参数的含义, 参数的类型int, 是否有默认值
| :param step:
| :return: 返回的结果的含义(时间), 返回数据的类型int
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| count = 1
Process finished with exit code 0
5 项目文档的生成
5.1 方式一:使用内置模块 pydoc
具体步骤:
- cd 进入对应工程目录下
-
python3 --help
查看python3可用参数使用说明 - 找到 -m 参数,通过 -m 可以通过脚本形式运行一个库模块
- 查看 pydoc 模块使用说明,
python3 -m pydoc -h
- 按如下参数,使用 pydoc
- 查看文档描述:
python3 -m pydoc 模块名称
- 启动本地服务, 浏览文档:
python3 -m pydoc -p 1234
- 生成指定模块html文档:
python3 -m pydoc -w 模块名称
5.2 方式二:借助其他三方库
- doxygen
- epydoc
- Sphinx
网友评论