Re: Why is this?



For the sake of completeness (I didn't copy this message to the list):

> I have a problem with initialization.
> >>> a, b = [[]]*2
> >>> a.append(1)
> >>> b
> [1]
>
> Why is this? Why does not this behave like the below:

You create a single list (inside your brachets) and duplicate a
reference to it. a and b are two references to the same list; this is
just a problem for mutable objects, but I think it's safe to say that
it bites everyone at some point.

> >>> [[], []] == [[]]*2
> True

Obviously, this is not a test for referential identity.

Hope this helps to clear the matter up!
Ole


2005/8/10, Jiri Barton <jbar@xxxxxxxxxxxx>:
> Yes, now it is clear!
>
> As always, I should have RTFM, the operator* is not just a syntactic sugar
> and thus does not make copies. You know, my actual scenario was with four
> variables at the time:
>
> a, b, c, d = [], [], [], []
>
> so I was even more tempted to use the previous and wrong approach:-)
>
> Thank you for your explanation. No more errors in my code from now on (oh
> well,...)
>
> jbar
> --
> http://mail.python.org/mailman/listinfo/python-list
>
.



Relevant Pages

  • Re: I think C# is forcing us to write more (redundant) code
    ... it has to point to an actual reference. ... >> locals here and locals are stack allocated, ... >> don't say "you should not assume that locals are compiler initialized'. ... >> I don't have a problem with this explicit initialization in the current ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reference to a reference?
    ... Or can it be done on the reference type also? ... There is situation where late initialization is necessary (due to ... a wrapper would be more convenient, because you could more easily make ... why does the hash set need to be updated automatically when an object is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: I think C# is forcing us to write more (redundant) code
    ... it has to point to an actual reference. ... > locals here and locals are stack allocated, ... > while the out keyword tells the compiler to take the address of the value as ... > I don't have a problem with this explicit initialization in the current ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reference to a reference?
    ... What's the point of storing a null reference in a HashSet? ... There is situation where late initialization is necessary, so I initially set it to null, but I know later at some time this object will eventually get initialized so I want the set to use the initialized object instead - hence the creation of Referenceclass. ... I still have no idea how the whole hash set thing works into your design. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Assigning to a reference
    ... > should this be a problem since I'm trying to assign to a reference? ... A reference cannot be made to "refer ... Once the initialization is complete, ... initialization stage you can bind a reference wherever you want to bind it. ...
    (comp.lang.cpp)