代码拉取完成,页面将自动刷新
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// ClassMethod objects
package py
var ClassMethodType = ObjectType.NewType("classmethod",
`classmethod(function) -> method
Convert a function to be a class method.
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:
def f(cls, arg1, arg2, ...): ...
f = classmethod(f)
It can be called either on the class (e.g. C.f()) or on an instance
(e.g. C().f()). The instance is ignored except for its class.
If a class method is called for a derived class, the derived class
object is passed as the implied first argument.
Class methods are different than C++ or Java static methods.
If you want those, see the staticmethod builtin.`, ClassMethodNew, nil)
type ClassMethod struct {
Callable Object
Dict StringDict
}
// Type of this ClassMethod object
func (o ClassMethod) Type() *Type {
return ClassMethodType
}
// Get the Dict
func (c *ClassMethod) GetDict() StringDict {
return c.Dict
}
// ClassMethodNew
func ClassMethodNew(metatype *Type, args Tuple, kwargs StringDict) (res Object, err error) {
c := &ClassMethod{}
err = UnpackTuple(args, kwargs, "classmethod", 1, 1, &c.Callable)
if err != nil {
return nil, err
}
return c, nil
}
// Read a classmethod from a class which makes a bound method
func (c *ClassMethod) M__get__(instance, owner Object) (Object, error) {
if owner == nil {
owner = instance.Type()
}
return NewBoundMethod(owner, c.Callable), nil
}
// Properties
func init() {
ClassMethodType.Dict["__func__"] = &Property{
Fget: func(self Object) (Object, error) {
return self.(*ClassMethod).Callable, nil
},
}
}
// Check interface is satisfied
var _ IGetDict = (*ClassMethod)(nil)
var _ I__get__ = (*ClassMethod)(nil)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。