These functions provide access to Python's built-in super()
mechanism
from within R, making it easier to work with Python class inheritance
when defining classes via reticulate::PyClass()
or py_class()
.
Value
py_super()
returns a Python object proxy to the superclass.py_super_init()
returnsNULL
invisibly (orreticulate::py_none()
ifconvert = FALSE
), called for side effects.
Details
py_super()
returns a proxy to the superclass of the current object, equivalent to Python'ssuper()
. It can be used in two forms:py_super()
(no arguments): behaves likesuper()
with implicit resolution provided byreticulate
.py_super(type, object_or_type)
(two arguments): advanced usage equivalent tosuper(type, object_or_type)
in Python, e.g.py_super(A, self)
orpy_super(__class__, self)
.
py_super_init()
directly calls the superclass initializer, equivalent tosuper().__init__(...)
. Unlikepy_super()
, it always refers to the next class in the MRO and does not accept type arguments.
While reticulate
internally injects a zero-argument super()
reference
into class methods, this re-export makes the functionality explicit and
convenient for user code.
Examples
if (FALSE) { # \dontrun{
Employee <- py_class(
"Employee",
`__init__` = function(self, name, id) {
self$name <- name
self$id <- id
return(py_builtins$None)
}
)
Salary <- py_class(
"Salary", inherit = Employee,
`__init__` = function(self, name, id, salary) {
# Option A: use implicit super
py_super()$`__init__`(name, id)
# Option B: use advanced two-argument super
# py_super(Salary, self)$`__init__`(name, id)
# Option C: use py_super_init()
# py_super_init(name, id)
self$salary <- salary
return(py_builtins$None)
}
)
} # }