Passing function arguments directly to cls()
This question already has an answer here:
Now that you've edited your question, it's clear what's happening. You're method has a the decorator @classmethod. According to the docs:
A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:
class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
This is (somewhat) equivalent to this:
class MyClass:
def f(self, arg1, arg2, ...):
cls = self.__class__
...
Classes in Python are callables (eg they can be invoked with Class()
). Those two things are therefore equivalent:
a = MyClass()
and
cls = MyClass
b = cls()
In summary, cls
is not a function, but a callable. Btw. you can make any object a callable by adding the special method __call__
:
class A:
def __call__(self):
print("Woah!")
a = A()
a() # prints Woah!
It would seem that the first argument is a class and the second is a set of key-value args (see this question describing the double star notation).
Inside the function the key-value args are then passed to the class constructor to make the variable thread.
链接地址: http://www.djcxy.com/p/9074.html上一篇: 命名这个python / ruby语言结构(使用数组值来满足函数参数)
下一篇: 直接将函数参数传递给cls()