Make a python class constructor

spec %py_class% body

Arguments

spec

a bare symbol MyClassName, or a call MyClassName(SuperClass)

body

an expression that can be evaluated to construct the class methods.

Value

The python class constructor, invisibly. Note, the same constructor is also assigned in the parent frame.

Examples

if (FALSE) { MyClass %py_class% { initialize <- function(x) { print("Hi from MyClass$initialize()!") self$x <- x } my_method <- function() { self$x } } my_class_instance <- MyClass(42) my_class_instance$my_method() MyClass2(MyClass) %py_class% { "This will be a __doc__ string for MyClass2" initialize <- function(...) { "This will be the __doc__ string for the MyClass2.__init__() method" print("Hi from MyClass2$initialize()!") super$initialize(...) } } my_class_instance2 <- MyClass2(42) my_class_instance2$my_method() reticulate::py_help(MyClass2) # see the __doc__ strings and more! }