实例化多个对象时重用的可选参数对象 [英] Optional parameter object re-used when instantiating multiple objects

查看:89
本文介绍了实例化多个对象时重用的可选参数对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


为什么设计python使b和c(根据下面的代码)

实际上共享相同的列表对象?对我来说似乎更自然的是

每个对象都会在点数

变量中使用新的列表对象创建。


class Blob:

def __init __(self,points = []):

self._points = points

b = Blob()

c = Blob()


b._points.append(1)

c._points.append(2)


打印b._points


#这将显示b._points与c._points是同一个对象


>

解决方案

Rick Giuly< rg ********** @ yahoo.comwrites:


Hello All,



Hello,


为什么python设计如此那个b和c(根据下面的代码)

实际上共享相同的列表对象?对我来说似乎更自然的是

每个对象都会在点数

变量中使用新的列表对象创建。


class Blob:

def __init __(self,points = []):

self._points = points


b = Blob( )

c = Blob()


b._points.append(1)

c._points.append(2)


打印b._points


#这将显示b._points与c._points相同的对象


这可能是MFAQ(大多数FAQ)!


查看 http://www.python.org/doc/faq/ (我不能指向

问题,因为今天早上我到美国的互联网管道非常生锈。


HTH


-

Arnaud


2008年11月15日星期六01:40:04 -0800,Rick Giuly写道:


你好A. ll,


为什么设计python使b和c(根据下面的代码)

实际上共享相同的列表对象?对我来说似乎更自然的是,每个对象都会在点数

变量中使用新的列表对象创建。



这根本不是天生的*。你每次使用相同的列表初始化参数points

。如果你想让它每次都有一个不同的列表

,你应该这么说。不要因为你告诉它做的事而责怪这个语言。


class Blob:

def __init __(self,points = []):

self._points = points



让's'分析一下。您创建一个方法__init__。该功能是* *
创建*一次*。作为创建函数的过程的一部分,

参数指向被赋予空列表的默认值。当创建方法时,该空列表的创建* * * * *
在函数体中,将_points属性设置为点。 />
当然它是相同的列表对象。


由于该方法只创建一次,因此

默认值很自然也只创建一次。如果你想在每次调用函数时创建

,你必须把它放在函数的

体内:


class Blob:

def __init __(self,points = None):

如果积分为无:

points = []

self._points = points


现在每次都将_points设置为一个唯一的空列表。


这与此不同:


alist = []

b1 = Blob(alist)

b2 = Blob (alist)


你会惊讶于b1和b2共享同一个列表吗?如果是,那么

你需要考虑Python是如何工作的,而不是你如何想象它的工作原理。


-

史蒂文


11月15日凌晨3点40分,Rick Giuly< rgiuly.gr ... @ yahoo.comwrote :


Hello all,


为什么设计python使b和c(根据下面的代码)

实际上共享相同的列表对象?对我来说似乎更自然的是

每个对象都会在点数

变量中使用新的列表对象创建。


class Blob:

* * def __init __(self,points = []):

* * * * self._points = points


b = Blob()

c = Blob()


b._points.append(1)

c._points .append(2)


打印b._points


#这将显示b._points与c._points $是同一个对象b $ b



嗨瑞克,


我不认为丹尼斯或史蒂文读得很好。你说

''为什么Python做X?''和''看起来很自然你不要X''。

Dennis和Steven都说, ''Python做X''。


史蒂文确实提出了建议答案。他说:


如果你想在每次调用函数时创建

,你必须把它放在

函数体:



认为这是真的,你的问题的答案是''因为

对象不是在函数体内创建的,''或'''因为

参数列表在函数体的外部''。


从你的帖子中,很难说这个''duh'' - 类型观察

会指出构造的显着特征,或者是否

你是在更深层次的事情之后。


如果你问,''为什么参数列表不被认为是在里面

身体?'',然后答案是,它几乎是随意的。

无论Python的作者选择哪一个,另一个'

解决方法将是平等的简而言之,语法上没有一个显然是




直到有人发给你链接到Python的作者的博客那个

给出了答案,为了让创建静态变量变得非常容易,

不要让他们告诉你。


Hello All,

Why is python designed so that b and c (according to code below)
actually share the same list object? It seems more natural to me that
each object would be created with a new list object in the points
variable.

class Blob:
def __init__(self, points=[]):
self._points = points
b = Blob()
c = Blob()

b._points.append(1)
c._points.append(2)

print b._points

# this will show that b._points is the same object as c._points



解决方案

Rick Giuly <rg**********@yahoo.comwrites:

Hello All,

Hello,

Why is python designed so that b and c (according to code below)
actually share the same list object? It seems more natural to me that
each object would be created with a new list object in the points
variable.

class Blob:
def __init__(self, points=[]):
self._points = points
b = Blob()
c = Blob()

b._points.append(1)
c._points.append(2)

print b._points

# this will show that b._points is the same object as c._points

This is probably the MFAQ (Most FAQ)!

Have a look in http://www.python.org/doc/faq/ (I can''t point at the
question as my internet pipes to the US are very rusty this morning)

HTH

--
Arnaud


On Sat, 15 Nov 2008 01:40:04 -0800, Rick Giuly wrote:

Hello All,

Why is python designed so that b and c (according to code below)
actually share the same list object? It seems more natural to me that
each object would be created with a new list object in the points
variable.

That''s not natural *at all*. You''re initialising the argument "points"
with the same list every time. If you wanted it to have a different list
each time, you should have said so. Don''t blame the language for doing
exactly what you told it to do.

class Blob:
def __init__(self, points=[]):
self._points = points

Let''s analyze this. You create a method __init__. That function is
created *once*. As part of the process of creating the function, the
argument "points" is given the default value of an empty list. The
creation of that empty list happens *once*, when the method is created.
In the body of the function, you set the _points attribute to points.
Naturally it is the same list object.

Since the method is only created once, it is only natural that the
default value is also only created once. If you want something to be
created each time the function is called, you have to put it inside the
body of the function:

class Blob:
def __init__(self, points=None):
if points is None:
points = []
self._points = points

Now you will have _points set to a unique empty list each time.

This is no different from doing this:

alist = []
b1 = Blob(alist)
b2 = Blob(alist)

Would you be surprised that b1 and b2 share the same list? If yes, then
you need to think about how Python really works, rather than how you
imagine it works.

--
Steven


On Nov 15, 3:40*am, Rick Giuly <rgiuly.gr...@yahoo.comwrote:

Hello All,

Why is python designed so that b and c (according to code below)
actually share the same list object? It seems more natural to me that
each object would be created with a new list object in the points
variable.

class Blob:
* * def __init__(self, points=[]):
* * * * self._points = points

b = Blob()
c = Blob()

b._points.append(1)
c._points.append(2)

print b._points

# this will show that b._points is the same object as c._points

Hi Rick,

I don''t think Dennis or Steven read your post very well. You said
''Why does Python do X?'', and ''It seems natural to you to do not X''.
Dennis and Steven both said, ''Python does X''.

Steven did get around to suggesting an answer though. He said:

If you want something to be
created each time the function is called, you have to put it inside the
body of the function:

Taking this to be true, the answer to your question is, ''Because the
object isn''t created inside the body of the function,'' or, ''Because
the argument list is outside the body of the function''.

From your post, it''s hard to tell whether this ''duh''-type observation
would point out the salient feature of the construct, or whether
you''re after something deeper.

If you''re asking, ''Why isn''t the argument list considered to be inside
the body?'', then the answer is, it''s pretty much arbitrary.
Regardless of which one the author of Python chose, the other''s
workaround would be equally short, and neither one is obviously
indicated by the syntax.

And until someone sends you a link to Python''s author''s blog that
gives the answer, ''To make creating static variables really easy'',
don''t let them tell you so.


这篇关于实例化多个对象时重用的可选参数对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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