C++ 与 Python 中的传递引用差异 [英] Pass-by-reference differences in C++ vs Python

查看:53
本文介绍了C++ 与 Python 中的传递引用差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点 C++ 背景.我目前正在学习 Python 并试图了解函数参数是如何传递的.我知道这个问题在这里被问过很多次,但由于我不是计算机科学专业的,很多讨论线程对我来说都相当深奥,另外,我还没有看到不同语言之间关于这个主题的很多比较,所以我想我会尝试一个新的帖子.

情况是这样的:我的理解是Python只是通过引用传递(即传递变量名的值,我相信是位置地址.)在这种印象下,我回到了我的旧C++教科书希望刷新我对传递引用如何工作的记忆.但后来我发现 C++ 中的引用传递似乎与 Python 中的引用传递不同.例如,变量重新分配会影响 C++ 中的原始变量,但不会影响 Python.

以下是我在 C++ 和 Python 中的实验代码和输出.C++ 代码来自 D.S. Malik 的 C++ Programming: From Problem Analysis to Program Design (6th Edition);Python 代码是我对 C++ 代码的翻译.请注意,C++ 示例同时包含值传递和引用传递.

那么,我的问题来了:传递引用语言的定义是特定于语言的吗?Python 参数传递是如何工作的?我刚才说的有什么不正确的逻辑吗?任何清晰简单的外行解释将不胜感激.

C++ 代码

//例6-13:引用和值参数#include 使用命名空间标准;void funOne(int a, int& b, char v);void funTwo(int& x, int y, char& w);int main(){int num1, num2;字符 ch;num1 = 10;//第1行num2 = 15;//第2行ch = 'A';//第3行cout<<"第 4 行:内部 main: num1 = " <<编号 1<<", num2 = " <<num2<<", 和 ch = "<<ch<<结束;//第4行funOne(num1, num2, ch);//第5行cout<<"第 6 行:funOne 之后:num1 = " <<编号 1<<", num2 = " <<num2<<", 和 ch = "<<ch<<结束;//第6行funTwo(num2, 25, ch);//第7行cout<<"第 8 行:在 funTwo 之后:num1 = " <<编号 1<<", num2 = " <<num2<<", 和 ch = "<<ch<<结束;//第8行返回0;}void funOne(int a, int& b, char v){一个;一个 = 一个;//第9行一个++;//第10行b = b * 2;//第11行v = 'B';//第12行cout<<"第 13 行:funOne 内部:a = " <<一种<<", b = " <<<<", v = " <<v<<", 一个 = " <<一个<<结束;//第13行}void funTwo(int& x, int y, char& w){x++;//第14行y = y * 2;//第15行w = 'G';//第16行cout<<"第 17 行:在 funTwo 中:x = " <<X<<", y = " <<y<<", 并且 w = " <<瓦<<结束;//第17行}

C++ 输出

<块引用>

内部主:num1 = 10,num2 = 15,ch = A
在 funOne 中:a = 11, b = 30, v = B, and one = 10
funOne 之后:num1 = 10,num2 = 30,ch = A
在 funTwo 中:x = 31,y = 50,w = G
funTwo 之后:num1 = 10,num2 = 31,ch = G

Python 代码

def funOne (a, b, v):一个 = 一个+= 1b = b * 2v = 'B'打印(里面funOne:a",a,b",b,v",v,一",一)def funTwo (x, y, w):x += 1y = y * 2w = 'G'打印(在funTwo里面:x",x,y",y,w",w)数量 1 = 10数量 2 = 15ch = 'A'打印(内部主要:num1",num1,num2",num2,ch",ch)funOne(num1, num2, ch)print("funOne 之后:num1", num1, "num2", num2, "ch", ch)funTwo(num2, 25, ch)print("在 funTwo 之后:num1", num1, "num2", num2, "ch", ch)

Python 输出

<块引用>

内部主:num1 10 num2 15 ch A
funOne 内部:a 11 b 30 v B one 10
funOne 之后: num1 10 num2 15 ch A
内部 funTwo:x 16 y 50 w G
funTwo 之后: num1 10 num2 15 ch A

谢谢.

解决方案

在 C++ 中最接近 Python 引用的是智能指针.事实上,CPython 使用类似于 std::shared_ptr 的引用计数指针.

这个 Python 函数:

def foo(an_int, a_string, list_of_int):

大致相当于:

variant foo(shared_ptr an_int, shared_ptr a_string, shared_ptr>>list_of_int) {

I have a little bit of C++ background. I am currently learning Python and am trying to understand how the function parameters are passed. I know this question is asked numerous times up here, but since I am not a Computer Science major, a lot of discussions threads are rather esoteric to me, plus, I've not seen a lot of comparisons on this topic between different languages, so I thought I'd give a new post a try.

The situation is this: My understanding is that Python only pass by reference (i.e. pass the value of the variable name, which I believe is the location address.) Under this impression, I went back to my old C++ textbook hoping to refresh my memory on how pass by reference works. But then I discovered that passing by reference in C++ seemed to work differently from passing by reference in Python. An example would be that variable reassignment affected the original variable in C++ but not in Python.

Below are codes and outputs of my experiments in C++ and in Python. The C++ code was from C++ Programming: From Problem Analysis to Program Design (6th Edition) by D.S. Malik; the Python code was my translation of the C++ code. Note that the C++ example contains both pass-by-value and pass-by-reference.

So, here come my questions: Is the definition of pass-by-reference language-specific? How does Python parameter passing work? Are there any incorrect logics in what I just said? Any clear and simple layman explanation would be greatly appreciated.

C++ code

//Example 6-13: Reference and value parameters

#include <iostream>

using namespace std;

void funOne(int a, int& b, char v);
void funTwo(int& x, int y, char& w);

int main()
{
    int num1, num2;
    char ch;
    num1 = 10; //Line 1
    num2 = 15; //Line 2
    ch = 'A'; //Line 3
    cout << "Line 4: Inside main: num1 = " << num1
        << ", num2 = " << num2 << ", and ch = "
        << ch << endl; //Line 4
    funOne(num1, num2, ch); //Line 5
    cout << "Line 6: After funOne: num1 = " << num1
        << ", num2 = " << num2 << ", and ch = "
        << ch << endl; //Line 6
    funTwo(num2, 25, ch); //Line 7
    cout << "Line 8: After funTwo: num1 = " << num1
        << ", num2 = " << num2 << ", and ch = "
        << ch << endl; //Line 8
    return 0;
}

void funOne(int a, int& b, char v)
{
    int one;
    one = a; //Line 9
    a++; //Line 10
    b = b * 2; //Line 11
    v = 'B'; //Line 12
    cout << "Line 13: Inside funOne: a = " << a
        << ", b = " << b << ", v = " << v
        << ", and one = " << one << endl; //Line 13
}

void funTwo(int& x, int y, char& w)
{
    x++; //Line 14
    y = y * 2; //Line 15
    w = 'G'; //Line 16
    cout << "Line 17: Inside funTwo: x = " << x
        << ", y = " << y << ", and w = " << w
        << endl; //Line 17
}

C++ output

Inside main: num1 = 10, num2 = 15, and ch = A
Inside funOne: a = 11, b = 30, v = B, and one = 10
After funOne: num1 = 10, num2 = 30, and ch = A
Inside funTwo: x = 31, y = 50, and w = G
After funTwo: num1 = 10, num2 = 31, and ch = G

Python code

def funOne (a, b, v):
    one = a
    a += 1
    b = b * 2
    v = 'B'
    print("Inside funOne: a", a, "b", b, "v", v, "one", one)

def funTwo (x, y, w):
    x += 1
    y = y * 2
    w = 'G'
    print("Inside funTwo: x", x, "y", y, "w", w)

num1 = 10
num2 = 15
ch = 'A'

print("Inside main: num1", num1, "num2", num2, "ch", ch)
funOne(num1, num2, ch)
print("After funOne: num1", num1, "num2", num2, "ch", ch)
funTwo(num2, 25, ch)
print("After funTwo: num1", num1, "num2", num2, "ch", ch)

Python output

Inside main: num1 10 num2 15 ch A
Inside funOne: a 11 b 30 v B one 10
After funOne: num1 10 num2 15 ch A
Inside funTwo: x 16 y 50 w G
After funTwo: num1 10 num2 15 ch A

Thanks.

解决方案

The closest thing to a Python reference in C++ is a smart pointer. In fact CPython uses reference counted pointers similar to std::shared_ptr.

This Python function:

def foo(an_int, a_string, list_of_int):

Is roughly equivalent to:

variant foo(shared_ptr<const int> an_int, shared_ptr<const string> a_string, shared_ptr<vector<shared_ptr<const int>>> list_of_int) {

这篇关于C++ 与 Python 中的传递引用差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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