equivalent to R's `do.call` in python

Deena

Is there an equivalent to R's do.call in python?

do.call(what = 'sum', args = list(1:10)) #[1] 55
do.call(what = 'mean', args = list(1:10)) #[1] 5.5

?do.call
# Description
# do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.
Martijn Pieters

There is no built-in for this, but it is easy enough to construct an equivalent.

You can look up any object from the built-ins namespace using the __builtin__ (Python 2) or builtins (Python 3) modules then apply arbitrary arguments to that with *args and **kwargs syntax:

try:
    # Python 2
    import __builtin__ as builtins
except ImportError:
    # Python 3
    import builtins

def do_call(what, *args, **kwargs):
    return getattr(builtins, what)(*args, **kwargs)

do_call('sum', range(1, 11))

Generally speaking, we don't do this in Python. If you must translate strings into function objects, it is generally preferred to build a custom dictionary:

functions = {
    'sum': sum,
    'mean': lambda v: sum(v) / len(v),
}

then look up functions from that dictionary instead:

functions['sum'](range(1, 11))

This lets you strictly control what names are available to dynamic code, preventing a user from making a nuisance of themselves by calling built-ins for their destructive or disruptive effects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Equivalent of R's createDataPartition in Python

From Dev

Equivalent of R's removeSparseTerms in Python

From Dev

equivalent of R's View for Python's pandas

From Dev

What is python's equivalent of R's NA?

From Dev

Python's equivalent for R's dput() function

From Dev

Is there a Python equivalent to R's sample() function?

From Dev

Python equivalent for R's 'zoo' package

From Dev

R's read.table equivalent in Python

From Dev

Python equivalent of R's head and tail function

From Dev

Python equivalent of R's rnbinom parametrized with mu

From Dev

simplest python equivalent to R's grepl

From Dev

simplest python equivalent to R's gsub

From Dev

Python equivalent to R 's factor data type

From Dev

simplest python equivalent to R's grepl

From Dev

Equivalent of R's sapply with a condition In Python

From Dev

Do JavaScript classes have a method equivalent to Python classes' __call__?

From Dev

Equivalent of R's paste command for vector of numbers in Python

From Dev

Is there a Python equivalent of R's str(), returning only the structure of an object?

From Dev

R's which() and which.min() Equivalent in Python

From Dev

Is there a python equivalent for R's h2o.stack?

From Dev

Java equivalent to python's "with"

From Dev

Equivalent of Python's 'with' in Julia?

From Dev

Equivalent of Python's locals()?

From Java

python equivalent of R table

From Dev

Python equivalent of the R operator "%in%"

From Dev

Equivalent of source() of R in Python

From Dev

Equivalent of "table" of R in python

From Dev

Python's equivalent of Ruby's ||=

From Dev

what is equivalent to do.call(rbind, list)?