8000 Implement classmethod() and staticmethod() · go-python/gpython@6bd194a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bd194a

Browse files
committed
Implement classmethod() and staticmethod()
1 parent d625bd9 commit 6bd194a

File tree

3 files changed

+134
-6
lines changed

3 files changed

+134
-6
lines changed

builtin/builtin.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ func init() {
6565
"bool": py.BoolType,
6666
// "memoryview": py.MemoryViewType,
6767
// "bytearray": py.ByteArrayType,
68-
"bytes": py.BytesType,
69-
// "classmethod": py.ClassMethodType,
70-
"complex": py.ComplexType,
71-
"dict": py.StringDictType, // FIXME
68+
"bytes": py.BytesType,
69+
"classmethod": py.ClassMethodType,
70+
"complex": py.ComplexType,
71+
"dict": py.StringDictType, // FIXME
7272
// "enumerate": py.EnumType,
7373
// "filter": py.FilterType,
7474
"float": py.FloatType,
@@ -82,8 +82,8 @@ func init() {
8282
// "reversed": py.ReversedType,
8383
"set": py.SetType,
8484
// "slice": py.SliceType,
85-
// "staticmethod": py.StaticMethodType,
86-
"str": py.StringType,
85+
"staticmethod": py.StaticMethodType,
86+
"str": py.StringType,
8787
// "super": py.SuperType,
8888
"tuple": py.TupleType,
8989
"type": py.TypeType,

py/classmethod.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// ClassMethod objects
2+
3+
package py
4+
5+
var ClassMethodType = ObjectType.NewType("classmethod",
6+
`classmethod(function) -> method
7+
8+
Convert a function to be a class method.
9+
10+
A class method receives the class as implicit first argument,
11+
just like an instance method receives the instance.
12+
To declare a class method, use this idiom:
13+
14+
class C:
15+
def f(cls, arg1, arg2, ...): ...
16+
f = classmethod(f)
17+
18+
It can be called either on the class (e.g. C.f()) or on an instance
19+
(e.g. C().f()). The instance is ignored except for its class.
20+
If a class method is called for a derived class, the derived class
21+
object is passed as the implied first argument.
22+
23+
Class methods are different than C++ or Java static methods.
24+
If you want those, see the staticmethod builtin.`, ClassMethodNew, nil)
25+
26+
type ClassMethod struct {
27+
Callable Object
28+
Dict StringDict
29+
}
30+
31+
// Type of this ClassMethod object
32+
func (o ClassMethod) Type() *Type {
33+
return ClassMethodType
34+
}
35+
36+
// Get the Dict
37+
func (c *ClassMethod) GetDict() StringDict {
38+
return c.Dict
39+
}
40+
41+
// ClassMethodNew
42+
func ClassMethodNew(metatype *Type, args Tuple, kwargs StringDict) (res Object) {
43+
c := &ClassMethod{}
44+
UnpackTuple(args, kwargs, "classmethod", 1, 1, &c.Callable)
45+
return c
46+
}
47+
48+
// Read a classmethod from a class which makes a bound method
49+
func (c *ClassMethod) M__get__(instance, owner Object) Object {
50+
if owner == nil {
51+
owner = instance.Type()
52+
}
53+
return NewBoundMethod(owner, c.Callable)
54+
}
55+
56+
// Properties
57+
func init() {
58+
ClassMethodType.Dict["__func__"] = &Property{
59+
Fget: func(self Object) Object {
60+
return self.(*ClassMethod).Callable
61+
},
62+
}
63+
}
64+
65+
// Check interface is satisfied
66+
var _ IGetDict = (*ClassMethod)(nil)
67+
var _ I__get__ = (*ClassMethod)(nil)

py/staticmethod.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// StaticMethod objects
2+
3+
package py
4+
5+
var StaticMethodType = ObjectType.NewType("staticmethod",
6+
`staticmethod(function) -> method
7+
8+
Convert a function to be a static method.
9+
10+
A static method does not receive an implicit first argument.
11+
To declare a static method, use this idiom:
12+
13+
class C:
14+
def f(arg1, arg2, ...): ...
15+
f = staticmethod(f)
16+
17+
It can be called either on the class (e.g. C.f()) or on an instance
18+
(e.g. C().f()). The instance is ignored except for its class.
19+
20+
Static methods in Python are similar to those found in Java or C++.
21+
For a more advanced concept, see the classmethod builtin.`, StaticMethodNew, nil)
22+
23+
type StaticMethod struct {
24+
Callable Object
25+
Dict StringDict
26+
}
27+
28+
// Type of this StaticMethod object
29+
func (o StaticMethod) Type() *Type {
30+
return StaticMethodType
31+
}
32+
33+
// Get the Dict
34+
func (c *StaticMethod) GetDict() StringDict {
35+
return c.Dict
36+
}
37+
38+
// StaticMethodNew
39+
func StaticMethodNew(metatype *Type, args Tuple, kwargs StringDict) (res Object) {
40+
c := &StaticMethod{}
41+
UnpackTuple(args, kwargs, "staticmethod", 1, 1, &c.Callable)
42+
return c
43+
}
44+
45+
// Read a staticmethod from a class - no bound method here
46+
func (c *StaticMethod) M__get__(instance, owner Object) Object {
47+
return c.Callable
48+
}
49+
50+
// Properties
51+
func init() {
52+
StaticMethodType.Dict["__func__"] = &Property{
53+
Fget: func(self Object) Object {
54+
return self.(*StaticMethod).Callable
55+
},
56+
}
57+
}
58+
59+
// Check interface is satisfied
60+
var _ IGetDict = (*StaticMethod)(nil)
61+
var _ I__get__ = (*StaticMethod)(nil)

0 commit comments

Comments
 (0)
0