函数参数的可变性? [英] Mutability of function arguments?

查看:121
本文介绍了函数参数的可变性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以接受参数的函数,比如foo和

吧,并修改foo和bar的原始副本以及它的本地

版本 - 相当于C ++ funct(& foo,& bar)。


我在这个新闻组和其他地方环顾四周,我收集了

这是Python中一个非常普遍的问题,但通常用b $ b回答不,你不能。干净,对吧?一些网站,新闻组

帖子等都建议人们要求更多的Pythonic等。

做事的方式;那么,是否存在一个或者至少一个不使用对象作为可变参数包装器的



实际上,那会是接近工作?声明:


class FooWrapper:

__init __(fooToLoad):

self.foo = fooToLoad


意味着我现在可以声明一个FooWrapper持有一个foo,将

FooWrapper传递给一个函数,并让函数以foo结束

现在修改了FooWrapper?


提前感谢每个人的时间;我希望我能理解。

I''m trying to create a function that can take arguments, say, foo and
bar, and modify the original copies of foo and bar as well as its local
versions -- the equivalent of C++ funct(&foo, &bar).

I''ve looked around on this newsgroup and elsewhere, and I gather that
this is a very common concern in Python, but one which is ordinarily
answered with "No, you can''t. Neat, huh?" A few websites, newsgroup
posts, etc. have recommended that one ask for a more "Pythonic" way of
doing things; so, is there one, or at least one that doesn''t involve
using objects as wrappers for mutable arguments?

And, indeed, would that approach work? Would declaring:

class FooWrapper :
__init__(fooToLoad) :
self.foo = fooToLoad

mean that I could now declare a FooWrapper holding a foo, pass the
FooWrapper to a function, and have the function conclude with the foo
within the FooWrapper now modified?

Thanks in advance for everyone''s time; I hope I''m comprehensible.

推荐答案

(Re。可变性问题:)


更新,没关系。我发现FooWrapper解决方案并不是那么糟糕

毕竟 - 甚至更好的是将这个变量完全放在

不同模块中。


但是,仍然欢迎任何想要回答这个问题的人。

很抱歉打扰,并且在我想到之前发布了......:)

(Re. mutability question:)

Update, never mind. I found that the FooWrapper solution isn''t so bad
after all -- and even better is putting the variable in question in a
different module entirely.

However, anyone who wants to answer the question is still welcome to.
Sorry to be a bother, and to have posted before I thought... :)


" ex_ottoyuhr" <前********* @ hotmail.com>写道:
"ex_ottoyuhr" <ex*********@hotmail.com> writes:
我正在尝试创建一个可以接受参数的函数,例如foo和
bar,并修改foo和bar的原始副本以及它的本地
版本 - 相当于C ++ funct(& foo,& bar)。


C ++'s''&''导致参数通过引用传递。 Python使用所有参数确实是
。您对

函数中的参数所做的任何更改都将在调用者中看到,除非您明确复制

来传递。

I我已经在这个新闻组和其他地方环顾四周,并且我认为这是Python中一个非常普遍的问题,但通常会回答不,你不能。干净,对吧?一些网站,新闻组
帖子等建议人们要求更多的Pythonic。做事的方式;那么,有没有一个,或者至少有一个不使用对象作为可变参数的包装器?


如果你的论点是可变的,你不需要做任何事情就可以改变它们 - 只需要调用mutator方法。如果您的论点

不可变,那么您无法在函数中更改它们,或者在原始命名空间中使用




如果您真正想要做的是在调用

命名空间中重新绑定一个变量 - 那么,你不能这样做。处理在C中传递引用的常用用法的标准方法是返回值(或者

值 - Python处理多值返回和赋值很多

比C ++更干净)并在调用时重新绑定变量。


如果你坚持用Python编写C / C ++,你可以包装一个不可变的

对象在一个类的实例中有一个方法可以让你改变它,因为

你建议:

事实上,这种方法是否可行?会声明:
类FooWrapper:
__init __(fooToLoad):
self.foo = fooToLoad
意味着我现在可以声明一个持有foo的FooWrapper,通过
FooWrapper到一个函数,并且现在FooWrapper中的函数结束了foo
I''m trying to create a function that can take arguments, say, foo and
bar, and modify the original copies of foo and bar as well as its local
versions -- the equivalent of C++ funct(&foo, &bar).
C++''s ''&'' causes an argument to be passed by reference. Python does
that with all arguments. Any changes you make to the argument in the
function will be seen in the caller unless you explicitly make a copy
to pass.
I''ve looked around on this newsgroup and elsewhere, and I gather that
this is a very common concern in Python, but one which is ordinarily
answered with "No, you can''t. Neat, huh?" A few websites, newsgroup
posts, etc. have recommended that one ask for a more "Pythonic" way of
doing things; so, is there one, or at least one that doesn''t involve
using objects as wrappers for mutable arguments?
If your arguments are mutable, you don''t need to do anything to be
able to change them - just call the mutator methods. If your arguments
aren''t mutable, then you can''t change them, either in the function or
in the original namespace.

If what you really want to do is rebind a variable in the calling
namespace - well, you can''t do that. The standard way to deal with the
usual uses for passing references in C is to return the value (or
values - Python handles multi-valued return and assignment much
cleaner than C++) and rebind the variables at the point of the call.

If you insist on writing C/C++ in Python, you can wrap an immutable
object in an instance of class with a method to let you change it, as
you suggest:
And, indeed, would that approach work? Would declaring:
class FooWrapper :
__init__(fooToLoad) :
self.foo = fooToLoad mean that I could now declare a FooWrapper holding a foo, pass the
FooWrapper to a function, and have the function conclude with the foo
within the FooWrapper now modified?




如果你喜欢扩展的lambda演算语法,你可以使用我的Ref

类: http ://aspn.activestate.com/ASPN/Coo.../Recipe/456150

< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



If you like an extended lambda calculus syntax, you can use my Ref
class: http://aspn.activestate.com/ASPN/Coo.../Recipe/456150.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


" ex_ottoyuhr" <前********* @ hotmail.com>在消息中写道

news:11 ********************* @ z14g2000cwz.googlegro ups.com ...
"ex_ottoyuhr" <ex*********@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
我正在尝试创建一个可以接受参数的函数,比如foo和
吧,并修改foo和bar的原始副本以及它的本地版本 - 等价物C ++ funct(& foo,& bar)。

我已经浏览了这个新闻组和其他地方,我收集到了这个,这是Python中一个非常普遍的问题,但通常会回答不,你不能。干净,对吧?一些网站,新闻组
帖子等建议人们要求更多的Pythonic。做事的方式;那么,是否有一个,或者至少有一个不使用对象作为可变论证的包装器?

实际上,这种方法会起作用吗?会声明:

类FooWrapper:
__init __(fooToLoad):
self.foo = fooToLoad

意味着我现在可以声明一个FooWrapper持有一个foo,将FooWrapper传递给函数,并在现在修改的FooWrapper中使用foo
结束函数?

提前感谢每个人的时间;我希望我能理解。
I''m trying to create a function that can take arguments, say, foo and
bar, and modify the original copies of foo and bar as well as its local
versions -- the equivalent of C++ funct(&foo, &bar).

I''ve looked around on this newsgroup and elsewhere, and I gather that
this is a very common concern in Python, but one which is ordinarily
answered with "No, you can''t. Neat, huh?" A few websites, newsgroup
posts, etc. have recommended that one ask for a more "Pythonic" way of
doing things; so, is there one, or at least one that doesn''t involve
using objects as wrappers for mutable arguments?

And, indeed, would that approach work? Would declaring:

class FooWrapper :
__init__(fooToLoad) :
self.foo = fooToLoad

mean that I could now declare a FooWrapper holding a foo, pass the
FooWrapper to a function, and have the function conclude with the foo
within the FooWrapper now modified?

Thanks in advance for everyone''s time; I hope I''m comprehensible.




Python不是C ++而且不需要通过修改

函数来返回多个值参数:



Python isn''t C++ and there is no need to return multiple values by modifying
function parameters:

def funct(a,b):
....返回+ 1 ,b + 1

.... foo,bar = 1,2
print foo,bar
2 3 foo,bar = funct(foo,bar)
print foo,bar
3 4
def funct(a,b): .... return a+1,b+1
.... foo,bar=1,2
print foo,bar 2 3 foo,bar=funct(foo,bar)
print foo,bar 3 4




-Mark



-Mark


这篇关于函数参数的可变性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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