python vs. R scope

r vs python 2019
python vs r popularity
python vs r speed
r vs python for machine learning
r vs python vs excel
data camp r vs python
data manipulation r vs python
kdnuggets r vs python

I am trying to understand why I get a different output in two different function in R vs the same(?) implementation in python.

python:

    def increment(n):
       n = n + 1
       print(n)

    n = 1
    increment(n)
    print(n)
2
1

    def increment2(x):
       x[0] = x[0] + 1
       print(x)

    n = [1]
    increment2(n)
    print(n)

2
2

R:

increment <- function(n){
  n = n + 1
  print(n) 
}

n = 1
increment(n)
2
print(n)
1

increment2 <- function(n){
  n[1] = n[1] + 1
  print(n)
}

n = c(1)
increment2(n)
2
print(n)
1

In my head it seems more consistent the R output. everything is inside the function and do not get outside (unless I return and assign the output back to n). Can anyone give me a pythonic interpretation of it?

I can't speak for how R passes parameters, but it's pretty common for programming languages (including Python) to have mutations on mutable objects be reflected outside of the function that performed the mutation. Java, C#, and other popular languages that support OOP (Object Oriented Programming) act this way too.

Lists like [1] are mutable objects, so you see that mutation outside of the function. This type of behavior makes object oriented programming much more convenient.

If this behavior is undesirable, consider using a functional programming style in python (immutable objects, map, filter, reduce) or passing copies of your mutable objects to your functions.

I don't think there's much going on here that has to do with it being pythonic or not. It's a language mechanism: nothing more.

Which has more scope python or R programming?, Why do people often ask R vs Python again and again instead of picking one of them you can see clearly the difference between the scope of R and Python. Again, there is more good news: Python programmers and R programmers borrow good ideas from each other a lot. For example, Python's plotnine data visualization package was inspired by R's ggplot2 package, and R's rvest web scraping package was inspired by Python's BeautifulSoup package. So eventually the best ideas from either language make

This can be interpreted in terms of object identity.

A list x in python is like a pointer in that it has an identity independent of its contents so assigning a new value to an element of a list does not change the identity of the list. Changing the contents in the function does not change the list's identity and it seems that a function is free to change the contents.

A vector in R does not have an identity apart from its contents. Changing the contents in the function creates a new vector. The original vector is unchanged. R does have objects which have object identity -- they are called environments.

increment3 <- function(e){
  e$n = e$n + 1
  print(e$n)
}

e <- new.env()
e$n <- 1
increment3(e)
## [1] 2
print(e$n)
## [1] 2

In R, it is also possible to modify a vector in place using external C or C++ code. For example, see https://gist.github.com/ggrothendieck/53811e2769d0582407ae

R vs Python: Usability, Popularity, Pros & Cons, Jobs & Salaries |, R or Python, which one should you learn? Here is a meta-review on R vs Python - usability, popularity index, advantages & limitations, job  R and Python are state of the art in terms of programming language oriented towards data science. Learning both of them is, of course, the ideal solution. R and Python requires a time-investment, and such luxury is not available for everyone. Python is a general-purpose language with a readable syntax.

R is heavily influenced by functional languages, most notably Scheme. In functional languages, a "function" is understood just like in mathematics, it does not (and cannot) change its arguments, and its output depends only on arguments (and nothing else).

# pseudocode
let x be 1
tell_me sin(x)   # 0.841
tell_me x   # still 1

It is conceivable that sin(x) would commit a sin (from a functional perspective) and assign a new value to x.

R is not a purely functional language, however.

(1) You can (easily, and sometimes with bad consequences) access objects from within a function.

> rm(jumbo) # if you're not running this for the first time
> mumbo <- function() jumbo
> mumbo()
Error in mumbo() : object 'jumbo' not found
> jumbo <- 1
> mumbo()
[1] 1

[edit] There was an objection in a comment that some objects need to be visible from within a function. That is completely true, for example, one cannot possibly define arithmetical operations in every function. So the definition of + must be accessible ... but the difference is, in some languages you have explicit control over what is accessible and what is not. I'm not a python expert but I guess that's what is meant by

 from jumbo import *

R has packages, which you can attach in a similar way but the difference is that everything in your workspace is, by default, visible from within a function. This may be useful but is also dangerous as you may inadvertently refer to objects that you forgot to define within a function ... and the thing will work in a wrong way, as in the following example:

X <- 1e+10
addone <- function(x) X + 1  # want to increment the argument by 1
addone(3)
# [1] 1e+10  
addone(3)==1e+10+1
# [1] TRUE   

This is avoided in packages, so a function in a package cannot accidentally get values from your global workspace. And if you are so inclined, you can change the environment of your own functions as well. This might be a way to prevent such accidental errors (not necessarily a convenient way, though):

environment(mumbo)  # .GlobalEnv
environment(mumbo) <- baseenv()  # changing the environment
mumbo()  # error: object 'jumbo' not found

[/edit]

(2) You can, if you want to, change outside objects from within a function, for example, with <<- (as opposed to <-):

> increment.n <- function(){
+   n <<- n + 1
+   print(n) 
+ }
> increment.n()
Error in increment.n() : object 'n' not found
> n <- 1
> increment.n()
[1] 2
> n
[1] 2
> 

R Vs Python: What's the Difference?, R and Python are both open-source programming languages with a large community. New libraries or tools are added continuously to their  In a nutshell, he says, Python is better for for data manipulation and repeated tasks, while R is good for ad hoc analysis and exploring datasets. He went with Python when working on elections coverage, since it was a relatively routine, predictable process.

Python vs R for AI, Machine Learning, and Data Science, This article covers Python vs R vs other languages for data science, of this is out of scope, there are a few characteristics worth mentioning. R vs Python is a common topic of debate for data scientists and data analysts these days. R and Python both are open source programming languages. Both languages are used in data science and have a lot of libraries. Python is a general-purpose programming language whereas R is used for statistical computing and graphics.

R or Python: What is the best programming language for data , Crunching numbers. R YOU SURE? If you want to upgrade your data analysis skills, which programming language should you learn? Comparing Python vs R Objectively. There have been dozens of articles written comparing Python vs R from a subjective standpoint. This article aims to look at the languages more objectively. We’ll analyze a data set side by side in Python and R, and show what code is needed in both languages to achieve the same result.

Python vs R: Compared and Contrasted, Python vs R for Data Science: Compared and Contrasted in academia and fairly popular in industry, both because of its scope and because it  Python vs. R for Data Analysis At DataCamp, we often get emails from learners asking whether they should use Python or R when performing their day-to-day data analysis tasks. Both Python and R are among the most popular languages for data analysis, and each has its supporters and opponents.

Comments
  • You may need n1 = copy.deepcopy(n) and then work on n1
  • google pass by value or reference to get an idea of what's going on
  • @keithpjolley I see...then am I right to think that python passes either by reference or by value depending on whether the object is mutable or not?
  • R passes most arguments by value. (There are a few exceptions, but arrays aren't one of them. Mutable objects in R are typically built on environments, which are an exception to the pass by value rule.)
  • Interesting statement: "R does have objects which have object identity -- they are called environments." Is this the way to define full-fledged objects in R?
  • Yes, the various object oriented systems in R use environments.
  • Okay, deleted my comment.
  • "Me too" is too short but that's what i did too
  • @user2554330 I edited my answer to include kind of an answer to your objection (thanks!)