avatarLaxfed Paulacy

Summary

The web content discusses Python's unique approach to variable assignment and function argument passing, distinguishing it from languages like C++ and Java, and explains that Python uses pass-by-assignment semantics.

Abstract

The article titled "PYTHON — Pass by Value and Reference in Python" clarifies the misconception that Python follows either pass-by-value or pass-by-reference paradigms. Instead, it introduces the concept of pass-by-assignment, where variables are references to objects, and reassignment creates new objects rather than modifying existing ones. The author emphasizes that when a function is called, the parameters are bound to the provided objects, and changes to these parameters do not affect the original objects passed in, unless the objects are mutable. In such cases, the function can alter the object's state, and the changes will persist after the function's execution. The article concludes by noting that understanding this behavior is crucial for writing effective Python code.

Opinions

  • The author suggests that Python's handling of variables and function arguments is distinct from languages like C++ and Java, which directly manipulate memory locations.
  • The article implies that Python's pass-by-assignment model simplifies the understanding of variable scope and object mutability within functions.
  • It is the author's view that the common terminology of pass-by-value and pass-by-reference does not accurately describe Python's argument passing, which is why pass-by-assignment is a more fitting term.
  • The author believes that the behavior of Python's variable assignment and parameter passing is a key concept that developers must grasp to effectively program in Python.

PYTHON — Pass by Value and Reference in Python

First, solve the problem. Then, write the code. — John Johnson

PYTHON — Step Out Over Python

Pass-By-Value vs Pass-By-Reference in Python

In Python, the concepts of pass-by-value and pass-by-reference are different from those in languages like C++ and Java due to the way Python handles variables.

In C++, when a variable is created, it refers to a specific memory location. Assigning a value to that variable writes the value to that location in memory, and when the variable is reassigned, the old value is overwritten with the new value.

Python, on the other hand, handles variables differently. When a statement like x = 5 is executed in Python, an object to represent the integer 5 is created, and x refers to that object. When x is reassigned to 10, a new object representing 10 is created, and x is made to refer to the new object.

This means that Python does not deal with memory locations in a direct way like C++ does.

In Python, the commonly used term for this type of argument passing is pass-by-assignment. Parameter names are bound to the object when the function is entered, and whatever object is provided becomes what the parameter refers to. When a variable is bound to an immutable object, the object cannot be changed, but the variable can be bound to a new object.

Let’s look at an example:

def f(parameter):
    print(parameter)
    parameter = 'foo'
    print(parameter)

x = 40
f(x)
print(x)

When f() is called with the parameter x, the function reassigns parameter to 'foo' and prints it. However, outside the scope of the function, x remains unchanged, still referring to the original object.

It’s important to note that in Python, a function cannot change the value of an argument by reassigning the corresponding parameter to something else. If the object is mutable, however, the function can make changes to the object it receives as an argument, and those changes will be reflected in the object that was passed to it when the function finishes its execution.

In the next lesson, we’ll explore how functions in Python can make changes to mutable objects.

This tutorial explained the concept of pass-by-assignment in Python, highlighting the differences between pass-by-value and pass-by-reference in Python compared to other programming languages like C++. It provided a clear example to illustrate the behavior of variables and parameters in Python functions.

PYTHON — Intro to Web Scraping with Python

Reference
Python
Value
Pass
ChatGPT
Recommended from ReadMedium