-
Notifications
You must be signed in to change notification settings - Fork 327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
append all Python class bases to R class attribute #26
Comments
Interesting, I was under the impression that |
It looks like |
Yeah, that's perfect. I get: devtools::install_github('rstudio/reticulate@930b283') library(reticulate)
gp <- import('GPflow')
k1 <- gp$kernels$Constant(1)
k2 <- gp$kernels$Bias(1)
k3 <- gp$kernels$Cosine(1)
class(k1); class(k2); class(k3)
Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When executing a python function with reticulate, class information from the python object is appended to the resulting R object's class attribute. This is done in the CPP function
py_ref
.If I understand this code correctly,
py_ref
grabs the__class__
attribute of the python object (the class of which this is an instance), as well as the parent class from which this class inherits, via the class object's.__bases__
attribute. However this is only done for the immediate parent class, not for the full inheritance.I am working on a port of the
GPflow
Python module, and want to map its nice kernel creation syntax to R. Kernel objects can be added, multiplied etc., and theGPflow
module enables this by defining addition for all objects of classKern
. for example, in python:The full class inheritance for these objects is:
k1
:object
>Parentable
>Parameterized
>Kern
>Static
>Constant
k2
:object
>Parentable
>Parameterized
>Kern
>Static
>Constant
>Bias
k3
:object
>Parentable
>Parameterized
>Kern
>Stationary
>Cosine
The
+
operator is a wrapper aroundGPflow.kernels.Add((k1, k2, k3))
, defined in theKern
class; it works for any class instance inheriting fromKern
.To enable the same syntax in an R port using reticulate, it would be really helpful for
GPflow.kernels.Kern
to be listed in the class attribute for the corresponding R objects, so we can do S3 dispatch. But because only the immediate parent class is returned this isn't currently possible. I.e. in R:A fix would be to have this section of
py_ref
recurse through the parent classes, finding their parents (via the.__bases__
attribute) and adding them to the R object's class attribute, until hitting eitherobject
or an empty string (object
's parent class).As far as I can tell, there is no sane way of doing this from the reticulate-wrapping GPflow-specific R package.
If that sounds like behaviour you would like (or tolerate) in
reticulate
, I'd be happy to do a PR (though my C++ is decidedly rusty so very happy if someone else wants to do it!).The text was updated successfully, but these errors were encountered: