Python 和引用传递.局限性? [英] Python and reference passing. Limitation?

查看:51
本文介绍了Python 和引用传递.局限性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做如下事情:

class Foo(object):
    def __init__(self):
        self.member = 10
        pass

def factory(foo):
    foo = Foo()

aTestFoo = None
factory(aTestFoo)

print aTestFoo.member

但是它崩溃了 AttributeError: 'NoneType' object has no attribute 'member':对象 aTestFoo 在函数 factory 的调用中没有被修改.

However it crashes with AttributeError: 'NoneType' object has no attribute 'member': the object aTestFoo has not been modified inside the call of the function factory.

pythonic 的执行方式是什么?这是一种避免的模式吗?如果是当前错误,怎么称呼?

What is the pythonic way of performing that ? Is it a pattern to avoid ? If it is a current mistake, how is it called ?

在 C++ 中,在函数原型中,我会添加对要在工厂中创建的指针的引用......但也许这不是我应该在 Python 中考虑的事情.

In C++, in the function prototype, I would have added a reference to the pointer to be created in the factory... but maybe this is not the kind of things I should think about in Python.

在 C# 中,有一个关键字 ref 允许修改引用本身,非常接近 C++ 的方式.我对 Java 一窍不通……但我确实对 Python 感到好奇.

In C#, there's the key word ref that allows to modify the reference itself, really close to the C++ way. I don't know in Java... and I do wonder in Python.

推荐答案

Python 没有通过引用传递.顺便说一下,这是它与 Java 共享的少数事情之一.有些人将 Python 中的参数传递描述为按值调用(并将值定义为引用,其中引用的含义与 C++ 中的含义不同),有些人将其描述为按引用传递,我发现其推理相当可疑(他们重新定义了它)使用 Python 所称的引用",并最终得到与几十年来所谓的引用传递"无关的东西),其他人则选择使用和滥用的术语(流行的例子是{pass,call} by {object,sharing}").请参阅 effbot.org 上的 Call By Object 以了解有关定义的相当广​​泛的讨论各种术语,关于历史,以及术语传递引用和值传递的一些论据中的缺陷.

Python does not have pass by reference. One of the few things it shares with Java, by the way. Some people describe argument passing in Python as call by value (and define the values as references, where reference means not what it means in C++), some people describe it as pass by reference with reasoning I find quite questionable (they re-define it to use to what Python calls "reference", and end up with something which has nothing to do with what has been known as pass by reference for decades), others go for terms which are not as widely used and abused (popular examples are "{pass,call} by {object,sharing}"). See Call By Object on effbot.org for a rather extensive discussion on the defintions of the various terms, on history, and on the flaws in some of the arguments for the terms pass by reference and pass by value.

没有命名的短篇故事是这样的:

The short story, without naming it, goes like this:

  • 每个变量、对象属性、集合项等.引用一个对象.
  • 赋值、参数传递等会创建另一个变量、对象属性、集合项等,它们引用同一个对象,但不知道哪些其他变量、对象属性、集合项等引用了该对象.
  • 任何变量、对象属性、集合项等均可用于修改对象,而任何其他变量、对象属性、集合项等均可用于观察该修改.
  • 没有变量、对象属性、集合项等引用另一个变量、对象属性、集合项等,因此您不能模拟通过引用传递(在 C++ 意义上),除非通过处理可变对象/集合作为您的命名空间".这太难看了,所以当有更简单的替代方法时不要使用它(例如返回值、异常或通过可迭代解包的多个返回值).

您可能认为这就像在 C 中使用指针,而不是指向指针的指针(但有时是指向包含指针的结构的指针).然后按值传递这些指针.但是不要过多地解读这个比喻.Python 的数据模型与 C 的有很大不同.

You may consider this like using pointers, but not pointers to pointers (but sometimes pointers to structures containing pointers) in C. And then passing those pointers by value. But don't read too much into this simile. Python's data model is significantly different from C's.

这篇关于Python 和引用传递.局限性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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