temp1 object exist only once.
temp1 variable is a reference to that object.
In temp.append(temp1) you add the reference as item to the list, so finally
you have list with 3 items: 3 references to the same object.
Now, when you change*) the object, it changes, and all variables
referencing it, will show new (same) content.
*)...and your last change is made in the last iteration of the cycle, where
you set =2 into all elements (of the single existing dictionary object).
In second case you create 3 different objects (dictionaries). You populate
the first with 0, second with 1, third with 2.
If you are not sure if you work with one object or more objects, use 'is'
operator, or 'id()' function.
'id()' returns unique id for each (different) object, 'is' return True if
both compared variables(or expressions) refer same object.
In first code example: temp[0] is temp[1], id(temp[0])==id(temp[1])
--both True
In second code example: temp[0] is temp[1], id(temp[0])==id(temp[1])
--both False
---------
It is a little dificult properly understand Python, because he has 2
different assignments.
Example:
variable = variable-or-expression
can do 2 different thinks. Maybe it would be better if there were 2
different operators, but it is just one: =
In first case (if the right side is not the variable): object is created
and reference to it saved to variable - Examples: dd=[1,2], ddd={},
stri=stri+'more-characters'
In second case (if the right side is variable): reference to (already
existing) object is saved to new variable - Examples: dd2=dd, stri2=stri,
stri4=stri3='some-text' (I mean for the left assignment)
If just one object exists, changing it using any of the variables (which
link it), will 'inline change' the object, i.e. change the content for all
variables. This is possible for mutable objects only. - Example:
dd2.append() will change dd too
But assign command never makes 'inline change', so with dd2=... you (link
it to existing or new created object, but you) never change dd.
Same happens when arguments are passed to function parameters.
Same happens when function return result from return command.
Plus you need know that default argument values for functions are created
when function object (and function variable) is created, i.e. NOT with each
call. If you 'inline change' such object, next call will start with changed
value!
------
So please fully understand what is here above. It is very difficult, but it
is not lot of information - and it is base of Python behavior.
Dne středa 1. dubna 2015 13:33:01 UTC+2 Ramos napsal(a):
>
> Please explain why this code
>
> temp=[]
> temp1={'a':0,'b':0,'c':0}
> for x in range(3):
> temp1['a']=x
> temp1['b']=x
> temp1['c']=x
> temp.append(temp1)
> print temp
>
>
> prints
>
> [{'a': 2, 'c': 2, 'b': 2}, {'a': 2, 'c': 2, 'b': 2}, {'a': 2, 'c': 2, 'b':
> 2}]
>
> instead of
>
> [{'a': 0, 'c': 0, 'b': 0}, {'a': 1, 'c': 1, 'b': 1}, {'a': 2, 'c': 2, 'b':
> 2}]
>
> i always did it like this
>
> temp=[]
>
> for x in range(3):
> *temp1={'a':0,'b':0,'c':0}*
> temp1['a']=x
> temp1['b']=x
> temp1['c']=x
> temp.append(temp1)
> print temp
>
> this prints it correctly but dont understand the diference
>
>
> Thank you
> António
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.