Table of Contents
Return result tuple
Example
Output
Passing mutable objects
Pass a mutated dictionary
Values ​​in class instances
Home Backend Development Python Tutorial How to write a function with output parameters (called by reference) in Python?

How to write a function with output parameters (called by reference) in Python?

Sep 02, 2023 pm 04:21 PM
functional programming Parameter passing call by reference

How to write a function with output parameters (called by reference) in Python?

All parameters (arguments) in the Python language are passed by reference. This means that if you change the reference content of a parameter in a function, that change will also be reflected in the calling function.

Achieve this by -

Return result tuple

Example

In this example we will return a tuple of results -

# Function Definition
def demo(val1, val2):
   val1 = 'new value'
   val2 = val2 + 1
   return val1, val2

x, y = 'old value', 5

# Function call
print(demo(x, y))
Copy after login

Output

('new value', 6)
Copy after login

Passing mutable objects

Example

In this example we will pass a mutable object -

# Function Definition
def demo2(a):
   # 'a' references a mutable list
   a[0] = 'new-value'
   # This changes a shared object
   a[1] = a[1] + 1

args = ['old-value', 5]
demo2(args)
print(args)
Copy after login

Output

['new-value', 6]
Copy after login

Pass a mutated dictionary

Example

In this example we will pass a dictionary -

def demo3(args):
   # args is a mutable dictionary
   args['val1'] = 'new-value'
   args['val2'] = args['val2'] + 1

args = {'val1': 'old-value', 'val2': 5}

# Function call
demo3(args)
print(args)
Copy after login

Output

{'val1': 'new-value', 'val2': 6}
Copy after login
Copy after login

Values ​​in class instances

Example

In this example we will pack the value in the class instance -

class Namespace:
   def __init__(self, **args):
      for key, value in args.items():
         setattr(self, key, value)

def func4(args):
   # args is a mutable Namespace
   args.val1 = 'new-value'
   args.val2 = args.val2 + 1

args = Namespace(val1='old-value', val2=5)

# Function Call
func4(args)
print(vars(args))
Copy after login

Output

{'val1': 'new-value', 'val2': 6}
Copy after login
Copy after login

The above is the detailed content of How to write a function with output parameters (called by reference) in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Best practices for optimizing Golang function parameter passing performance Best practices for optimizing Golang function parameter passing performance Apr 13, 2024 am 11:15 AM

In order to optimize Go function parameter passing performance, best practices include: using value types to avoid copying small value types; using pointers to pass large value types (structures); using value types to pass slices; and using interfaces to pass polymorphic types. In practice, when passing large JSON strings, passing the data parameter pointer can significantly improve deserialization performance.

Phalcon function of PHP function Phalcon function of PHP function May 20, 2023 am 08:06 AM

PHP is a powerful programming language that is widely used in web development. Phalcon is a high-performance full-stack framework based on PHP. Its unique feature is that many extension modules are written in C language, which greatly improves the performance of the framework. This article will focus on the function library in Phalcon and explore its application in PHP development. Phalcon function library is a large and comprehensive function library, including many commonly used PHP functions, such as string functions, array functions, date functions, etc. besides,

What is the parameter passing method for PHP functions? What is the parameter passing method for PHP functions? Apr 10, 2024 am 11:06 AM

There are two ways to pass parameters in PHP: call by value (the parameter is passed as a copy of the value, modification within the function does not affect the original variable) and passing by reference (the address of the parameter is passed, modification within the function will affect the original variable), when the original variable needs to be modified Use reference passing when calculating the shopping cart total price, which requires reference passing to calculate correctly.

How to pass parameters to PHP function? How to pass parameters to PHP function? Apr 10, 2024 pm 05:21 PM

PHP functions can pass values ​​through parameters, which are divided into pass by value and pass by reference: pass by value: modification of parameters within the function will not affect the original value; pass by reference: modification of parameters within the function will affect the original value. In addition, arrays can also be passed as parameters for operations such as calculating the sum of data.

Detailed explanation of C++ function parameters: Comparison of parameter passing methods of different pointer types Detailed explanation of C++ function parameters: Comparison of parameter passing methods of different pointer types Apr 27, 2024 am 09:27 AM

There are three ways to pass pointer parameters in C++: passing by value, passing by reference, and passing by address. Passing by value copies the pointer without affecting the original pointer; passing by reference allows the function to modify the original pointer; passing by address allows the function to modify the value pointed to by the pointer. Choose the appropriate parameter transmission method according to your needs.

Research on parameter passing methods in Go language Research on parameter passing methods in Go language Apr 03, 2024 pm 02:48 PM

In the Go language, there are two main ways to pass function parameters: value passing: passing a copy of the variable will not affect the original variable in the calling code. Pointer passing: Passing the address of a variable allows the function to directly modify the original variable in the calling code.

Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Apr 27, 2024 pm 02:09 PM

In a multi-threaded environment, function parameter passing methods are different, and the performance difference is significant: passing by value: copying parameter values, safe, but large objects are expensive. Pass by reference: Passing by reference is efficient, but function modifications will affect the caller. Pass by constant reference: Pass by constant reference, safe, but restricts the function's operation on parameters. Pass by pointer: Passing pointers is flexible, but pointer management is complex, and dangling pointers or memory leaks may occur. In parallel summation, passing by reference is more efficient than passing by value, and passing by pointer is the most flexible, but management is complicated.

Golang formal parameter requirements guide: parameter passing methods, value passing and address passing Golang formal parameter requirements guide: parameter passing methods, value passing and address passing Mar 02, 2024 pm 05:18 PM

Guide to Golang formal parameter requirements: parameter passing methods, value passing and address passing. In the process of learning the Golang programming language, it is very important to understand the parameter passing methods and the concepts of value passing and address passing. This article will delve into the formal parameter requirements in Golang, including the difference between parameter passing methods, value passing and address passing, and provide specific code examples to help readers better understand. 1. Parameter passing methods In Golang, there are two methods of parameter passing for functions: passing by value and passing by address. Pass by value (pass a copy): When the function is called, the actual

See all articles