__call__类的方法 [英] __call__ method of type class
问题描述
从所有方面我了解Python面向对象的编程,如果一个类定义了__call__
方法,而如果我们将类的实例用作函数调用,则将调用该方法.例如:
From all I understand about Python object oriented programming if a class has __call__
method defined that would be invoked if we use the instance of the class like a function call. For example:
class Employee:
def __init__(self,name,sal):
self.name = name
self.salary = sal
def __call__(self,value):
return self.salary * value
e = Employee("Subhayan",20000)
print (e(10))
因此__call__
方法将对象实例作为第一个参数.
So the __call__
method takes the object instance as the first argument.
我只是想了解Python中的元类,并且我读到type
是所有用户定义类的默认元类.
I am just trying to understand metaclasses in Python and I read that type
is the default metaclass of all user defined classes.
如果我在Python中定义了基本的自定义元类:
If I define a basic custom metaclass in Python:
class Meta(type):
def __new__(meta, classname, supers, classdict):
# Run by inherited type.__call__
return type.__new__(meta, classname, supers, classdict)
现在,按照本书中给出的文档,元类__new__
方法将由从类型继承的__call__
方法运行.
Now as per the documentation given in the book the metaclass __new__
method would be run by the __call__
method inherited from type.
现在我的问题是使用任何类的__call__
方法,我们必须拥有该类的对象,然后将其作为函数调用.
Now my question is to use the __call__
method of any class we have to have an object of that class and then call it as a function.
在这里,我们没有任何类型的对象可以使用其__call__
函数.
Here we don't have any object of type to use its __call__
function.
有人可以向我解释一下类型类的__call__
函数如何出现吗?
Can someone please explain to me how is the __call__
function of type class coming into picture?
推荐答案
任何类本身都是类型"类型的实例-因此,调用"一个类只是在其类上调用方法__call__
-碰巧是类型的__call__
.
type.__call__
的作用正好是:
在类似这样的代码上:
Any class is itself an instance of type "type" - therefore "calling" a class just calls the method __call__
on its class - which happens to be type's __call__
.
The effect of type.__call__
is exactly:
on code like:
class A:
pass
b = A()
-
type.__call__
接收类A
本身作为其第一个参数. - 它调用
A.__new__
-用伪代码我们可以将instace = A.__new__(cls)
编写为运行内容. - 返回"A"类的实例
- 然后在实例(
instance.__init__()
)上调用__init__
- ...并返回该实例
return instance
type.__call__
receives the classA
itself as its first parameter.- It calls the
A.__new__
- in pseudocode we could writeinstace = A.__new__(cls)
as what runs. - That returns an instance of the "A" class
- Then it calls
__init__
on the instance(instance.__init__()
) - ...and returns that instance
return instance
但是,如果类本身是从类型"派生的-即,它是新"类的实例化,则需要执行额外的步骤:魔术__class__
变量的特殊值填充在类方法-如果这些方法中的任何一个使用对super
的调用.在Python 3.6上,需要执行以下两个步骤:调用定义__set_name__
方法的任何类属性,并调用该类的__init_subclass__
方法.
However, if the the class itself is derived from "type" - i.e., it is the instantiation of a "new" class, extra steps are taken: The special value of the magic __class__
variable is filled in any of the class methods - if any of those methods use a call to super
. And on Python 3.6, these 2 further steps: any of the class attributes that define a __set_name__
method are called, and the class's __init_subclass__
method is called.
这篇关于__call__类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!