Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 996250d

Browse files
authored
Added self and self_class to super (#3691)
1 parent 23bb966 commit 996250d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

extra_tests/snippets/builtin_super.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test_super_list = super(list)
2+
assert test_super_list.__self__ is None
3+
assert test_super_list.__self_class__ is None
4+
assert test_super_list.__thisclass__ == list
5+
6+
7+
class testA:
8+
a = 1
9+
10+
11+
class testB(testA):
12+
b = 1
13+
14+
15+
superB = super(testB)
16+
assert superB.__thisclass__ == testB
17+
assert superB.__self_class__ is None
18+
assert superB.__self__ is None

vm/src/builtins/super.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ impl PySuper {
114114
self.typ.clone()
115115
}
116116

117+
#[pyproperty(magic)]
118+
fn self_class(&self) -> Option<PyTypeRef> {
119+
Some(self.obj.as_ref()?.1.clone())
120+
}
121+
122+
#[pyproperty]
123+
fn __self__(&self) -> Option<PyObjectRef> {
124+
Some(self.obj.as_ref()?.0.clone())
125+
}
126+
117127
#[pymethod(magic)]
118128
fn repr(&self) -> String {
119129
let typname = &self.typ.name();

0 commit comments

Comments
 (0)