首先来看一个函数间的调用
类方法:
执行结果:
metaclass能有什么用处,先来个感性的认识:
1.1 在wiki上面,metaclass是这样定义的:In object-oriented programming,
a metaclass is a class whose instances are classes.
Just as an ordinary class defines the behavior of certain objects,
a metaclass defines the behavior of certain classes and their instances.
也就是说metaclass的实例化结果是类,而class实例化的结果是instance。我是这么理解的:
metaclass是类似创建类的模板,所有的类都是通过他来create的(调用 new ),这使得你可以自由的控制
创建类的那个过程,实现你所需要的功能。
当然你也可以用函数的方式(下文会讲)
4.1 用类的形式
4.1.1 类继承于type, 例如: class Meta(type):pass
4.1.2 将需要使用metaclass来构建class的类的 metaclass 属性(不需要显示声明,直接有的了)赋值为Meta(继承于type的类)
4.2 用函数的形式
4.2.1 构建一个函数,例如叫metaclass_new, 需要3个参数:name, bases, attrs,
name: 类的名字
bases: 基类,通常是tuple类型
attrs: dict类型,就是类的属性或者函数
4.2.2 将需要使用metaclass来构建class的类的 metaclass 属性(不需要显示声明,直接有的了)赋值为函数metaclas_new
5.1 basic
metaclass的原理其实是这样的:当定义好类之后,创建类的时候其实是调用了type的 new 方法为这个类分配内存空间,创建
好了之后再调用type的 init 方法初始化(做一些赋值等)。所以metaclass的所有magic其实就在于这个 new 方法里面了。
说说这个方法: new (cls, name, bases, attrs)
cls: 将要创建的类,类似与self,但是self指向的是instance,而这里cls指向的是class
name: 类的名字,也就是我们通常用类名. name 获取的。
bases: 基类
attrs: 属性的dict。dict的内容可以是变量(类属性),也可以是函数(类方法)。
所以在创建类的过程,我们可以在这个函数里面修改name,bases,attrs的值来自由的达到我们的功能。这里常用的配合方法是
getattr和setattr(just an advice)
下面实现python中在一个类中调用另一个类的函数方法
或者下面来一个号理解的例子
执行结果:
先来介绍内部类与外部类是什么?
看源码解析:
内部类调用外部类的类属性和类方法
参考文献1
参考文献2
参考文献3
这里先肯定的回答一下:可以
python里方法在类中是作为类的属性的,在解释之前,这边先给个例子
>>>class Pizza(object):... radius = 42
... def __init__(self, size=10):
... self.size = size
... def get_size(self):
... return self.size
... @staticmethod
... def mix_ingredients(x, y):
... return x + y
... def cook(self):
... return self.mix_ingredients(self.cheese, self.vegetables)
... @classmethod
... def get_radius(cls):
... return cls.radius
>>> Pizza.get_size
<unbound method Pizza.get_size>
>>> Pizza.get_size()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method get_size() must be called with Pizza instance as first argument (got nothing instead)
>>> Pizza.get_size(Pizza(42))
42
>>> Pizza(42).get_size
<bound method Pizza.get_size of <__main__.Pizza object at 0x7f3138827910>>
>>> Pizza(42).get_size()
42
>>> m = Pizza(42).get_size
>>> m()
42
>>> m = Pizza(42).get_size
>>> m.__self__
<__main__.Pizza object at 0x7f3138827910>
>>> m == m.__self__.get_size
True
>>> Pizza().cook is Pizza().cook
False
>>> Pizza().mix_ingredients is Pizza.mix_ingredients
True
>>> Pizza().mix_ingredients is Pizza().mix_ingredients
True
>>> Pizza.get_radius
<bound method type.get_radius of <class '__main__.Pizza'>>
>>> Pizza().get_radius
<bound method type.get_radius of <class '__main__.Pizza'>>
>>> Pizza.get_radius is Pizza().get_radius
True
>>> Pizza.get_radius()
42
在上面的例子中可以看出python中类有三种方法,分别是类方法,静态方法,实例方法。而能让类只接调用的只有类方法,或通过一些小技巧,类也可以调用实例方法如上面例子中的调用
>>> Pizza.get_size(Pizza(42))42
这边顺便说明下这三中方法的区别
1类方法的特点是类方法不属于任何该类的对象,只属于类本身
2类的静态方法类似于全局函数,因为静态方法既没有实例方法的self参数也没有类方法的cls参数,谁都可以调用
3.实例方法只属于实例,是实例化的对象才能调用