Python和C++参考

发布于 2025-01-29 11:11:01 字数 778 浏览 2 评论 0 原文

我对C ++中的参考及其与Python的重叠有疑问。我在Python中写了一个功能,该功能吸收X和Y,并将Y添加到X中。此后,我打印了结果。

def func(x, y):
    x = x + y
    return x

c = [5,4,6,4]
d = [6,7,8,9]
t = func(c,d)
print(t)
print(c)

当Python通过引用通过所有内容时,人们会期望变量C会更改并包含d。但是,事实并非如此。 要查看这是否对C ++也是如此,我编写了一个向量函数,将两个向量添加在一起。

vector<int> FuncAdd(vector<int> &x, vector<int> &y){

x.insert(x.end(), y.begin(), y.end());

return x;
}

int main(){
vector<int> y = {3,4,5,6};
vector<int> x = {4,5,6,7};
vector<int> c = FuncAdd(x, y);

for(int i : x){  
    cout << i << " ";
}
cout << endl;
for(int i : c){  
    cout << i << " ";
}
cout << endl;

这里的结果是X确实改变了,现在包含y。为什么C ++会更改向量,而Python不会更改其列表?谢谢!

I have a question about references in c++ and its overlap to python. I have written a function in python that takes in x and y and adds y to x. Thereafter, I print the outcome.

def func(x, y):
    x = x + y
    return x

c = [5,4,6,4]
d = [6,7,8,9]
t = func(c,d)
print(t)
print(c)

As python passes everything by reference, one would expect that the variable c would change and contain d. However, this is not the case.
To see if this also holds true for c++, I have written a vector function that adds two vectors together.

vector<int> FuncAdd(vector<int> &x, vector<int> &y){

x.insert(x.end(), y.begin(), y.end());

return x;
}

int main(){
vector<int> y = {3,4,5,6};
vector<int> x = {4,5,6,7};
vector<int> c = FuncAdd(x, y);

for(int i : x){  
    cout << i << " ";
}
cout << endl;
for(int i : c){  
    cout << i << " ";
}
cout << endl;

The outcome here is that x does change and now contains y. Why does c++ change the vector and python does not change its list? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界和平 2025-02-05 11:11:01

在Python中,当您使用+用于串联两个列表创建新列表并返回时。这意味着,当您编写 X + Y 时,会创建一个新列表。这也意味着在 x = x + y 之后,本地变量 x 现在将参考新创建的列表(由于 x + y )。如果要更改原始列表在场,则可以使用扩展方法,如下示例中所述。

#--------v--v------------->x and y are local variables that refer to the passed objects
def func(x, y):
    
#-------vvvvv------------->this x + y creates a new list
    x = x + y
#---^--------------------->the "local variable x" on the left hand side now refers to the new list object created as a result of x + y
    return x

c = [5,4,6,4]
d = [6,7,8,9]


t = func(c,d) #here t refers to the object returned by the call to func which essentially was created from x + y 
print(t)      #prints the object return by the call to func [5, 4, 6, 4, 6, 7, 8, 9]
print(c)      #prints c which still refers to the original list [5, 4, 6, 4]

请注意,而不是写 x = x + y ,然后返回 x 您可以只写了返回x + y ,效果将相同,如下所示:

#--------v--v------------->x and y are local variables that refer to the passed objects
def func(x, y):

    return x +y #creates and returns a new list object

c = [5,4,6,4]
d = [6,7,8,9]


t = func(c,d) #here t refers to the object returned by the call to func which essentially was created from x + y 
print(t)      #prints the object return by the call to func [5, 4, 6, 4, 6, 7, 8, 9]
print(c)      #prints c which still refers to the original list [5, 4, 6, 4]

如果要更改原始列表在场您可以使用List的 Extend 方法,如下所示:

#--------v--v------------->x and y are local variables that refer to the passed objects
def func(x, y):
#-----vvvvvv-------------->changes x in-place instead of creating a new list
    x.extend(y); 

c = [5,4,6,4]
d = [6,7,8,9]

#no need to do the assignment here because the changes made to the list inside func will be reflected in the original list as we have used extend method
func(c,d) 
print(c)      #prints c [5, 4, 6, 4, 6, 7, 8, 9]

类似地,在C ++中,您正在使用< code> std :: vector :: insert std :: vector 的成员函数将插入元素被调用的向量(这是vector , X )。而且,由于您已通过引用传递了向量 X ,因此更改将反映在原始矢量中。

In Python, when you use + for concatenating two lists a new list is created and returned. This means that when you wrote x + y, a new list is created. This also means that after x = x + y, the local variable x will now refer to the newly created list(as a result of x + y). If you want to change the original list in-place you could instead use the extend method as explained in the below example.

#--------v--v------------->x and y are local variables that refer to the passed objects
def func(x, y):
    
#-------vvvvv------------->this x + y creates a new list
    x = x + y
#---^--------------------->the "local variable x" on the left hand side now refers to the new list object created as a result of x + y
    return x

c = [5,4,6,4]
d = [6,7,8,9]


t = func(c,d) #here t refers to the object returned by the call to func which essentially was created from x + y 
print(t)      #prints the object return by the call to func [5, 4, 6, 4, 6, 7, 8, 9]
print(c)      #prints c which still refers to the original list [5, 4, 6, 4]

Note instead of writing x = x + y and then returning x you could just have written return x + y and the effect will be the same, as shown below:

#--------v--v------------->x and y are local variables that refer to the passed objects
def func(x, y):

    return x +y #creates and returns a new list object

c = [5,4,6,4]
d = [6,7,8,9]


t = func(c,d) #here t refers to the object returned by the call to func which essentially was created from x + y 
print(t)      #prints the object return by the call to func [5, 4, 6, 4, 6, 7, 8, 9]
print(c)      #prints c which still refers to the original list [5, 4, 6, 4]

If you want to change the original list in-place you can use list's extend method as shown below:

#--------v--v------------->x and y are local variables that refer to the passed objects
def func(x, y):
#-----vvvvvv-------------->changes x in-place instead of creating a new list
    x.extend(y); 

c = [5,4,6,4]
d = [6,7,8,9]

#no need to do the assignment here because the changes made to the list inside func will be reflected in the original list as we have used extend method
func(c,d) 
print(c)      #prints c [5, 4, 6, 4, 6, 7, 8, 9]

Similarly, in C++ you're using the std::vector::insert member function of std::vector that will insert the elements into the vector on which the member function was called(which is the vector x). And since you've passed the vector x by reference, the change will be reflected in the original vector.

你不是我要的菜∠ 2025-02-05 11:11:01

是的,是的,在调用函数时,在Python中,您会获得对呼叫者中相同对象的引用。如果修改该对象,则在函数返回后会看到该修改。

但是语句 x = x + y di 不是修改 x ,它创建了一个全新的参考 x 替换从函数开始的那个点开始的旧一个。

Yes it's true, in Python when you call a function you get a reference to the same object that was in the caller. If you modify that object, you'll see that modification after the function returns.

But the statement x = x + y does not modify x, it creates a brand new reference x that replaces the old one from that point of the function onwards.

扛刀软妹 2025-02-05 11:11:01

在Python中,对象引用按值传递。

您将其称为“ C”

因此,您的列表是一个对象[5,4,6,4],当将变量C传递到函数时,

,该对象按值传递,但它将是相同的对象。因此,x是一个不同的变量,但包含相同的对象,直到...

您与另一个对象分配x
即x+y
现在,X具有不同的对象,与C的不同之处不一样

,Append()会起作用,因为它本质上是对同一对象的操作。

如果仍然不清楚,请参阅下面的链接
通过

In python Object references are passed by value.

So your list is an object [5,4,6,4] and you gave it the name 'c'

When you pass variable c to a function, the object is passed by value but it will be the same object.

So x is a different variable but contains the same object, until...

You assign x with another object
i.e. x+y
Now x has a different object, not the same as what c has

Thats why, append() would work because it is essentially an operation on the same object.

Refer to below link if it is still unclear
Python pass by value or reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文