Skip to content

Commit

Permalink
provide call() method for callable python objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Feb 10, 2017
1 parent 5bf8a6d commit d16d51b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 6 additions & 0 deletions R/python.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ str.python.builtin.module <- function(object, ...) {
if (!is.null(module))
return(module)
}

# convert 'call' to '__call__' if we aren't masking an underlying 'call'
if (identical(name, "call") &&
!py_has_attr(x, "call") && py_has_attr(x, "__call__")) {
name <- "__call__"
}

# default handling
attrib <- py_get_attr(x, name)
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,14 @@ b <- iterate(iter) # results are empty since items have already been drained
Callable Objects
----------------

In addition to acessing their methods and properites, some Python objects are also callable (meaning they can be invoked with parameters just like an ordinary function). Callable Python objects are returned to R as objects rather than functions (so you can interact with their methods and properties). However, you can still gain access to the callable function using the `as.function` generic method. For example:
In addition to acessing their methods and properites, some Python objects are also callable (meaning they can be invoked with parameters just like an ordinary function). Callable Python objects are returned to R as objects rather than functions, however, you can still execute the callable function via the `$call()` method, for example:

``` r
# get a callable object
parser <- spacy$English()

# call the object as a function
parser_fn <- as.function(parser)
parser_fn(text)
parser$call(spacy)
```

Advanced Functions
Expand Down
5 changes: 2 additions & 3 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,14 @@ b <- iterate(iter) # results are empty since items have already been drained

## Callable Objects

In addition to acessing their methods and properites, some Python objects are also callable (meaning they can be invoked with parameters just like an ordinary function). Callable Python objects are returned to R as objects rather than functions (so you can interact with their methods and properties). However, you can still gain access to the callable function using the `as.function` generic method. For example:
In addition to acessing their methods and properites, some Python objects are also callable (meaning they can be invoked with parameters just like an ordinary function). Callable Python objects are returned to R as objects rather than functions, however, you can still execute the callable function via the `$call()` method, for example:

```{r}
# get a callable object
parser <- spacy$English()
# call the object as a function
parser_fn <- as.function(parser)
parser_fn(text)
parser$call(spacy)
```

## Advanced Functions
Expand Down

0 comments on commit d16d51b

Please sign in to comment.