函数参数范围python 2.5.2 [英] function parameter scope python 2.5.2

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

问题描述



我最近遇到了一些有趣的行为,看起来像一个bug

给我,但我找不到适当的参考任何规格

来澄清它是否是一个bug。


这里是用来演示这个问题的示例代码:


class SomeObject (对象):


def __init __(自我):

self.words = [''one'',''two'',''三'',''四'',''五'']

def main(self):

recursive_func(self.words)

print self.words


def recursive_func(words):

if len(words)0:

word = words.pop()

print" Popped:%s" %word

recursive_func(words)

else:

print" Done"


if __name__ ==''__ main__'':

weird_obj = SomeObject()

weird_obj.main()

输出为:


弹出:五个

弹出:四个

弹出:三个

弹出:两个

弹出:一个

完成

[]


当然我希望recursive_func()会收到一份副本

weird_obj.words但似乎很乐意修改对象。


当然,解决方法是明确创建对象的副本

属性befor将它传递给recursive_func,但是如果它在类的各个部分内使用超过

可能会变得混乱。


有什么想法?我疯了,这应该是python的工作方式吗?


I recently encountered some interesting behaviour that looks like a bug
to me, but I can''t find the appropriate reference to any specifications
to clarify whether it is a bug.

Here''s the example code to demonstrate the issue:

class SomeObject(object):

def __init__(self):
self.words = [''one'', ''two'', ''three'', ''four'', ''five'']

def main(self):
recursive_func(self.words)
print self.words

def recursive_func(words):
if len(words) 0:
word = words.pop()
print "Popped: %s" % word
recursive_func(words)
else:
print "Done"

if __name__ == ''__main__'':
weird_obj = SomeObject()
weird_obj.main()
The output is:

Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it''s used more than
once inside various parts of the class that could get messy.

Any thoughts? Am I crazy and this is supposed to be the way python works?

推荐答案

J Kenneth King< ja *** @ agentultra.comwrites:
J Kenneth King <ja***@agentultra.comwrites:

我最近遇到了一些有趣的行为,看起来像一个bug

给我,但我找不到适当的参考任何规格

来澄清它是否是一个bug。


这里是用来演示这个问题的示例代码:


class SomeObject(object):


def __init __(self):

self.words = [''one'',''two'' ,''三'',''四'',''五'']

def main(self):

recursive_func(self.words )

print self.words


def recursive_func(words):

if len(words)0:

word = words.pop()

print" Popped:%s" %word

recursive_func(words)

else:

print" Done"


if __name__ ==''__ main__'':

weird_obj = SomeObject()

weird_obj.main()


输出为:


弹出:五个

弹出:四个

弹出:三个

弹出:两个

弹出:一个

完成

[]


当然我期望recursive_func()会收到

weird_obj.words的副本,但似乎很乐意修改对象。


当然,解决方法是明确创建对象的副本

属性befor将它传递给recursive_func,但是如果它在类的各个部分中使用的时间超过

可能会变得混乱。


有什么想法吗?我疯了,这应该是python的工作方式吗?
I recently encountered some interesting behaviour that looks like a bug
to me, but I can''t find the appropriate reference to any specifications
to clarify whether it is a bug.

Here''s the example code to demonstrate the issue:

class SomeObject(object):

def __init__(self):
self.words = [''one'', ''two'', ''three'', ''four'', ''five'']

def main(self):
recursive_func(self.words)
print self.words

def recursive_func(words):
if len(words) 0:
word = words.pop()
print "Popped: %s" % word
recursive_func(words)
else:
print "Done"

if __name__ == ''__main__'':
weird_obj = SomeObject()
weird_obj.main()
The output is:

Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it''s used more than
once inside various parts of the class that could get messy.

Any thoughts? Am I crazy and this is supposed to be the way python works?



当然,提供一个浅(或必要的深)副本使它工作,我很好奇为什么作为参数传递给类外的

函数的值传递给引用而不是副本。

Of course, providing a shallow (or deep as necessary) copy makes it
work, I''m curious as to why the value passed as a parameter to a
function outside the class is passed a reference rather than a copy.


11月21日,9:40 * am,J Kenneth King< ja ... @ agentultra.comwrote:
On Nov 21, 9:40*am, J Kenneth King <ja...@agentultra.comwrote:

当然,提供一个浅的(或必要的深层)副本使它成为

工作,我很好奇为什么将作为参数传递给类外的

函数的值传递给引用而不是副本。
Of course, providing a shallow (or deep as necessary) copy makes it
work, I''m curious as to why the value passed as a parameter to a
function outside the class is passed a reference rather than a copy.



你既没有传递参考也没有传递副本,你传递了

对象(在这种情况下是一个列表)直接:

http:// effbot。 org / zone / call-by-object.htm


11月20日下午6:40 *,J Kenneth King< ja ... @ agentultra.comwrote:
On Nov 20, 6:40*pm, J Kenneth King <ja...@agentultra.comwrote:

J Kenneth King< ja ... @ agentultra.comwrites:
J Kenneth King <ja...@agentultra.comwrites:

我最近遇到了一些有趣的行为,看起来像一个bug

给我,但我找不到适当的参考任何规格

来澄清它是否是一个bug 。
I recently encountered some interesting behaviour that looks like a bug
to me, but I can''t find the appropriate reference to any specifications
to clarify whether it is a bug.


这里是用来演示问题的示例代码:
Here''s the example code to demonstrate the issue:


class SomeObject(object):
class SomeObject(object):


* * def __init __(self):

* * * * self。 words = [''one'',''two'',''three'',''four'',''five'']
* * def __init__(self):
* * * * self.words = [''one'', ''two'', ''three'', ''four'', ''five'']


* * def main(self):

* * * * recursive_func(self.words)

* * * * print self.words
* * def main(self):
* * * * recursive_func(self.words)
* * * * print self.words


def recursive_func(words):

* * if len(words)0:

* * * * word = words.pop()

* * * * print" Popped:%s" %word

* * * * recursive_func(words)

* * else:

* * * * print" Done"
def recursive_func(words):
* * if len(words) 0:
* * * * word = words.pop()
* * * * print "Popped: %s" % word
* * * * recursive_func(words)
* * else:
* * * * print "Done"


if __name__ ==''__ main__'':

* * weird_obj = SomeObject()

* * weird_obj.main()
if __name__ == ''__main__'':
* * weird_obj = SomeObject()
* * weird_obj.main()


输出为:
The output is:


弹出:五个

弹出:四个

弹出:三个

弹出:两个

弹出:一个

完成

[]
Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]


当然我希望recursive_func()会收到

weird_obj.words的副本,但似乎很乐意修改对象。
Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.


当然,解决方法是显式创建对象的副本

属性将其传递给recursive_func,但是如果在课程的各个部分内部使用超过

,那可能会变得混乱。
Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it''s used more than
once inside various parts of the class that could get messy.


有什么想法吗?我疯了,这应该是python的工作方式吗?
Any thoughts? Am I crazy and this is supposed to be the way python works?



当然,提供一个浅的(或必要的深层)副本使它工作,我很好奇为什么作为参数传递给类外的

函数的值是传递引用而不是副本。


Of course, providing a shallow (or deep as necessary) copy makes it
work, I''m curious as to why the value passed as a parameter to a
function outside the class is passed a reference rather than a copy.



默认为什么它应该是副本?在Python中,所有副本必须是明确的



George

Why should it be a copy by default ? In Python all copies have to be
explicit.

George


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

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