将参数传递给函数 - (基本原理令我困惑) [英] Passing arguments to function - (The fundamentals are confusing me)

查看:87
本文介绍了将参数传递给函数 - (基本原理令我困惑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,当有人将参数传递给函数时,有人会介意给我一个简单的介绍如何引用Python的工作吗?下面的

代码应该突出我特定的困惑:


< code>


bool1 = True

lst1 = [1,2,3]


def func1(arg1):arg1.append(4)


def func2(arg1):arg1 = False

Hey guys, would someone mind giving me a quick rundown of how
references work in Python when passing arguments into functions? The
code below should highlight my specific confusion:

<code>

bool1=True
lst1=[1,2,3]

def func1(arg1): arg1.append(4)

def func2(arg1): arg1=False

func1(lst1)
lst1
[1,2,3, 4]

func2(bool1)
bool1
func1(lst1)
lst1 [1,2,3,4]
func2(bool1)
bool1



True


< / code>

为什么我的列表变量会在程序的其余部分被更改,但

我的布尔变量不会。我不理解什么?


-

Gregory Pi?ero

首席创新官

混合技术

www.blendedtechnologies.com

推荐答案

Gregory Pi?ero写道:
Gregory Pi?ero wrote:
嘿伙计们,有人会介意给我一个如何快速概述
将参数传递给函数时,引用在Python中工作?下面的代码应该突出我特定的困惑:


所有参数都是通过引用传递的,但在Python中,相等的重新绑定

这个名称。 />
< code>

bool1 = True
lst1 = [1,2,3]

def func1(arg1):arg1。 append(4)


在C ++中,假装它有动态类型,这相当于:

void func1(* arg1){

arg1->追加(4);

}

def func2(arg1):arg1 = False
void func2(* arg2){

arg2 =&(False);

为什么我的列表变量会在程序的其余部分被更改,但
我的布尔变量不会。我不明白的是什么?
Hey guys, would someone mind giving me a quick rundown of how
references work in Python when passing arguments into functions? The
code below should highlight my specific confusion:
All arguments are passed by reference, but in Python equality rebinds
the name.

<code>

bool1=True
lst1=[1,2,3]

def func1(arg1): arg1.append(4)
In C++, pretending it had dynamic typing, this would be equivalent to:
void func1( * arg1){
arg1->append(4);
}

def func2(arg1): arg1=False void func2 ( * arg2) {
arg2 = &(False);
Why does my list variable get changed for the rest of the program, but
my boolean variable doesn''t. What am I not understanding?




在Python中,x = y具有非常明确的含义y被分配给

名称的x。这个改变不会影响x中的任何内容,因为它开始

,它肯定不会影响其他任何对前面称为x的对象持有

的引用。<相比之下,调用一个改变对象的函数(比如.append

在列表上,或者由lst [index] = something赋值)实际上改变了

对象本身,这当然反映在所有其他名称中,这些名称包含

a对象的引用。



In Python, "x = y" has a very definite meaning of "y is assigned to the
name of x." This change does not affect whatever was in x to start
with, and it certainly would not affect anything else which holds a
reference to the object formerly known as x.

In contrast, calling a function which mutates the object (like .append
on lists, or assignment by lst[index]=something) actually changes the
object itself, which is of course reflected by all other names that hold
a reference to the object.


Christopher Subich写道:
Christopher Subich wrote:
Gregory Pi?ero写道:
Gregory Pi?ero wrote:
嘿伙计们,有人会介意给我一个关于
如何在Python中工作的快速概述将参数传递给函数时?下面的
代码应该突出我特定的困惑:


这个URL总是被抛出:

http://starship.python.net/crew/mwh/...jectthink.html
Hey guys, would someone mind giving me a quick rundown of how
references work in Python when passing arguments into functions? The
code below should highlight my specific confusion:
This URL is always tossed out:

http://starship.python.net/crew/mwh/...jectthink.html
所有参数都是通过引用传递的,但是在Python中,相等的名称重新绑定了



Bingo

为什么我的列表变量会在程序的其余部分被更改,但
我的布尔变量不是。我不理解的是什么?
All arguments are passed by reference, but in Python equality rebinds
the name.
Bingo
Why does my list variable get changed for the rest of the program, but
my boolean variable doesn''t. What am I not understanding?




布尔值是不可变的,列表是可变的。你改变(变异)相同的

列表,但是你引用了一个不同的(不可变的)Bool

在Python中,x = y具有非常明确的含义y被分配给x的名称。



Booleans are immutable, lists are mutable. You change (mutate) the same
list, but you are referencing a different (immutable) Bool
In Python, "x = y" has a very definite meaning of "y is assigned to the
name of x."




将其更改为y引用的对象是分配给x",

的名称,你更接近真相。



Change it to "the object referenced by y is assigned to the name of x",
and you''re closer to the truth.


>在Python等式中重新绑定名称


赋值(=)重新绑定名称。平等(==)完全是另外的东西



> in Python equality rebinds the name

Assignment (=) rebinds the name. Equality (==) is something else
entirely.


这篇关于将参数传递给函数 - (基本原理令我困惑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆