On Nov 15, 3:40 am, Rick Giuly <[EMAIL PROTECTED]> 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.
>
> 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.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to