dvd8719
dvd8719

Reputation: 58

Does python has an equivalent to symbol objects in R language?

I have a background in regular programming languages such as python and C++. I have recently started using R language for some data analysis and I am having a hard time comprehending what are symbol and expression objects in R and why there is no need for an equivalent object in python or C++.

See the sample code below in R language:

z <- expression(x+y)
x <- 1
y <- 2
eval(z)

Upvotes: 1

Views: 581

Answers (3)

Mikko Marttila
Mikko Marttila

Reputation: 11908

Expressions in R are (primarily) used to write functions that capture their input without evaluating it straight away [1]. A typical use case is to then change the context in which the captured input is evaluated. This type of “non-standard evaluation” can make interactive analysis code faster and more natural to write.

Here’s an example that takes a data argument and evaluates the second argument inside it (essentially the base function with()):

inside <- function(data, expr) {
  eval(substitute(expr), data)
}

df <- data.frame(x = c(1, 2), y = c(3, 4))

Without the ability to capture expressions, the following would fail, as x and y are not defined in the global scope, but are only present as columns in the data frame df:

inside(df, x + y)
#> [1] 4 6

I'm not aware of this concept existing in Python, and neither do I feel confident to comment on whether or not there is "no need for [it]" there; but I hope this helps with understanding how they fit in R.

For more details, a good place to start would be the metaprogramming section in Advanced R.

Upvotes: 2

augustomen
augustomen

Reputation: 9759

In Python, you can use Lambda expressions, which are actually one-line functions:

In [1]: z = lambda: x + y

In [2]: x = 1

In [3]: y = 2

In [4]: z()
Out[4]: 3

In [5]: x = 4

In [6]: z()
Out[6]: 6

The above declaration of z is equivalent to:

def z():
    return x + y

Because they are functions, scope rules apply. Therefore, z() only worked because x and y are global variables.

Upvotes: 1

Onyambu
Onyambu

Reputation: 79318

this is a mathematical expression or rather a R syntatically correct expression that can be evaluated. I do not know of an equivalent in python, since the parsing and evaluation is done with the function eval while in R the parsing and evaluation take two stages.

Take for example

python:

x = 1
y = 2
eval("x+y")
Out[376]: 3

Now with R, to do the same, we have:

x = 1
y = 2
z = parse(text="x+y")
eval(z)
[1] 3

You can see that the difference between the two is the parsing part. where in python, that is done within the eval function while in R, this is done independently.

Now the resulting object of parsing is what is called an expression:

class(z)
[1] "expression"
 typeof(z)
[1] "expression"

As you can see, the output of parsing to get the specified language's object is just an expression. This, when evaluated, gives you the object with respect to that language. Since eval in python does both parsing and evaluation into python objects, I do not think there is equivalent of expression in python

Upvotes: 4

Related Questions