Skip to contents

This function works like reticulate::PyClass(), but the defs argument can now be provided as individual arguments (...) instead of a list.

Usage

py_class(classname, ..., inherit = NULL, convert = FALSE)

Arguments

classname

Character. Name of the class.

...

Functions, attributes, or other definitions for the class.

inherit

List. A list of Python class objects representing the parent classes.

convert

Boolean. Whether to automatically convert Python objects to R.

Value

A Python class object.

Details

The original reticulate::PyClass() does not allow disabling the automatic conversion of Python objects to R. This function modifies its function body to enable that functionality.

Examples


if (FALSE) { # \dontrun{
Employee <- py_class("Employee",
                     `__init__` = function(self, name, id) {
                       self$name <- name
                       self$id <- id
                       return(invisible(NULL))
                     },
                     get_email = function(self) {
                       paste0(self$name, "_", self$id, "@company.com")
                     })
Mike <- Employee("Mike", "1234")
Mike$get_email()
Mike$get_email() |> class()
} # }