- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python 类与元类的深度挖掘 II【经验】由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
上一篇解决了通过调用类对象生成实例对象过程中可能遇到的命名空间相关的一些问题,这次我们向上回溯一层,看看类对象本身是如何产生的.
我们知道 type() 方法可以查看一个对象的类型,或者说判断这个对象是由那个类产生的:
1
2
3
4
5
6
7
8
9
|
print
(
type
(
12
))
print
(
type
(
'python'
))
class
A:
pass
print
(
type
(A))
|
通过这段代码可以看出,类对象 A 是由type() 产生的,也就是说 type 也可以用来产生新的对象,而且产生的是类对象,因此它是所有类对象的类:
1
2
3
4
5
6
7
|
print
(
type
.__doc__)
type
(object_or_name, bases,
dict
)
type
(
object
)
-
> the
object
's
type
type
(name, bases,
dict
)
-
> a new
type
|
。
class 定义类的语法实际上转化为 type(name, bases, dict),其中 name 参数为类的名字,bases 为继承父类的元组,dict 为类的属性和方法:
。
1
2
3
4
5
6
7
|
class
A:
pass
# 实际上等于
B
=
type
(
'A'
, (), {})
print
(A.__name__
=
=
B.__name__)
True
|
。
理论上说这就是元类的意义,但从实际的角度出发显然使用 class 语法更方便、合理,而元类的实际意义则是通过继承 type 类来构造一个新的元类,并进行特定的操作以产生具有特定行为的类对象。这样看来它的本质与普通的类对象没有差异,只不过继承的是 type 类.
在生成实例时是通过调用 __init__ 方法进行初始化的,而实际上在此之前会先调用 __new__ 方法用于创建实例,再通过 __init__ 初始化,就好像 __new__ 负责声明变量,而 __init__ 负责对声明的变量进行初始化一样。这里有一个规则是 __new__(cls,) 的返回值必须是 cls 参数的实例,否则 __init__ 将不会触发,例如在 enum.Enum 的定义中,由于枚举类型是单例模式,因此在定义 __new__ 的时候没有返回其实例,也就不会进行初始化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class
Enum:
def
__new__(
cls
, value):
print
(
cls
, value)
return
value
def
__init__(
self
):
print
(
"Will not be called!"
)
e
=
Enum(
1
)
<
class
'__main__.Enum'
>
1
|
通常情况下自己定义 __new__ 需要通过调用父类的 __new__ 方法创建一个 cls 的实例,同样在定义元类的时候则是调用上面提到的 type 的用法(因为元类继承自 type):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
class
MetaEnum(
type
):
def
__new__(metaclass, name, base, attrs):
print
(
"Metaclass: {}\nName: {}\nParents: {}\nAttributes: {}"
.
format
(metaclass, name, base, attrs))
return
super
().__new__(metaclass, name, base, attrs)
class
Enum(metaclass
=
MetaEnum):
# Python 2.7 中定义元类的方法是使用 __metaclass__ 变量
# [PEP 3115](https://www.python.org/dev/peps/pep-3115/)
# 将 Python 3.0 以后语法改为 class Cls(metaclass=Meta)
test
=
0
Metaclass:
Name: Enum
Parents: ()
Attributes: {
'__qualname__'
:
'Enum'
,
'__module__'
:
'__main__'
,
'test'
:
0
}
此时我们再来看 Enum 的类,已经不再是
type
而是其元类 MetaEnum:
type
(Enum)
__main__.MetaEnum
|
除了 __new__ 方法之外,PEP 3115 还定义了 __prepare__ 属性,用于设定初始化的命名空间(即 type 的第 3 个参数),还是以 enum.Enum 为例,我们需要限制枚举类型中属性名称不得重复使用,则可以通过元类限制类的行为:
# 定义新的字典类,在赋值新的 dict[k] = v 时 。
# 检查 k 是否重复 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
class
_EnumDict(
dict
):
def
__init__(
self
):
super
().__init__()
self
.members
=
[]
def
__setitem__(
self
, k, v):
if
k
in
self
.members:
raise
TypeError(
"Attempted to reuse key: '{}'"
.
format
(k))
else
:
self
.members.append(k)
super
().__setitem__(k, v)
class
MetaEnum(
type
):
@classmethod
def
__prepare__(metaclass,
cls
, bases):
return
_EnumDict()
def
__new__(metaclass, name, base, attrs):
return
super
().__new__(metaclass, name, base, attrs)
class
Enum(metaclass
=
MetaEnum):
pass
class
Color(Enum):
try
:
red
=
1
red
=
2
except
TypeError:
# 这里没有使用 as err: 的原因是?
print
(
"TypeError catched"
)
TypeError catched
|
Python 中一切皆为对象,所有的对象都是某一类的实例,或是某一元类的实例,type 是自己的元类也是自己的实例 。
最后此篇关于Python 类与元类的深度挖掘 II【经验】的文章就讲到这里了,如果你想了解更多关于Python 类与元类的深度挖掘 II【经验】的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在使用python 2.7 当我尝试在其上运行epsilon操作时出现此错误, 这是我的代码 import cv2 import numpy as np img = cv2.imread('img
1 很多程序员对互联网行业中广泛讨论的“35岁危机”表示不满,似乎所有的程序员都有着35岁的职业保质期。然而,随着AI技术的兴起,这场翻天覆地的技术革命正以更加残酷且直接的方式渗透到各行各业。程序员
我有一个包含多个子模块的项目,我想列出每个子模块的相对深度 该项目: main_project submodule1 submodule1\submodule1_1 submo
我有一张彩色图像及其深度图,它们都是由 Kinect 捕获的。我想将它投影到另一个位置(以查看它在另一个视角下的样子)。由于我没有 Kinect 的内在参数(相机参数);我该如何实现? P.S:我正在
给出了这三个网址: 1) https://example.com 2) https://example.com/app 3) https://example.com/app?param=hello 假
这个着色器(最后的代码)使用 raymarching 来渲染程序几何: 但是,在图像(上图)中,背景中的立方体应该部分遮挡粉红色实体;不是因为这个: struct fragmentOutput {
我希望能够在 ThreeJS 中创建一个房间。这是我到目前为止所拥有的: http://jsfiddle.net/7oyq4yqz/ var camera, scene, renderer, geom
我正在尝试通过编写小程序来学习 Haskell...所以我目前正在为简单表达式编写一个词法分析器/解析器。 (是的,我可以使用 Alex/Happy...但我想先学习核心语言)。 我的解析器本质上是一
我想使用像 [parse_ini_file][1] 这样的东西。 例如,我有一个 boot.ini 文件,我将加载该文件以进行进一步的处理: ;database connection sett
我正在使用 Mockito 来测试我的类(class)。我正在尝试使用深度 stub ,因为我没有办法在 Mockito 中的另一个模拟对象中注入(inject) Mock。 class MyServ
我试图在调整设备屏幕大小时重新排列布局,所以我这样做: if(screenOrientation == SCREEN_ORIENTATION_LANDSCAPE) { document
我正在 Ubuntu 上编写一个简单的 OpenGL 程序,它使用顶点数组绘制两个正方形(一个在另一个前面)。由于某种原因,GL_DEPTH_TEST 似乎不起作用。后面的物体出现在前面的物体前面
static FAST_FUNC int fileAction(const char *pathname, struct stat *sb UNUSED_PARAM, void *mo
我有这样的层次结构: namespace MyService{ class IBase { public: virtual ~IBase(){} protected: IPointer
我正在制作一个图片库,需要一些循环类别方面的帮助。下一个深度是图库配置文件中的已知设置,因此这不是关于无限深度循环的问题,而是循环已知深度并输出所有结果的最有效方法。 本质上,我想创建一个 包含系统中
如何以编程方式在树状结构上获取 n 深度迭代器?在根目录中我有 List 每个节点有 Map> n+1 深度。 我已修复 1 个深度: // DEPTH 1 nodeData.forEach(base
我正在构建一个包含大量自定义元素的 Polymer 单页界面。 现在我希望我的元素具有某种主样式,我可以在 index.html 或我的主要内容元素中定义它。可以这样想: index.html
我正在尝试每 25 秒连接到配对的蓝牙设备,通过 AlarmManager 安排,它会触发 WakefulBroadcastReceiver 以启动服务以进行连接。设备进入休眠状态后,前几个小时一切正
假设有一个有默认值的函数: int foo(int x=42); 如果这被其他人这样调用: int bar(int x=42) { return foo(x); } int moo(int x=42)
是否可以使用 Javascript 获取 url 深度(级别)? 如果我有这个网址:www.website.com/site/product/category/item -> depth=4www.w
我是一名优秀的程序员,十分优秀!