Re: [Tutor] When is = a copy and when is it an alias

2014-01-29 Thread spir

On 01/29/2014 02:34 AM, Denis Heidtmann wrote:

Glad to hear it.  That is what I was hoping, but I did not want to question
a helpful person.


(you could & should, we need helpful feedback too, to improve our skills; i 
mean, as long as it's honest indeed)


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-29 Thread Denis Heidtmann
On Tue, Jan 28, 2014 at 5:19 PM, Alan Gauld wrote:

> On 28/01/14 19:00, Denis Heidtmann wrote:
>
>> On Tue, Jan 28, 2014 at 12:28 AM, spir >
>
> This is getting confusing with two times Denis!
>
>  > wrote:
>>
>> a = [1,[2,3]]
>>
>
> I think the above line is a mistake. If Denis(spir) had missed
> this out his code would work as he describes, but with it he
> reassigns 'a' to a new list and, as you say breaks the connection.
>
> If you forget about that line and keep a pointing at the original
> [1,[1,2]] list then the following code makes sense.
>
>
>  a[0] = 0
>> b
>>
>> [0, [1, 2]]  # this is where I get lost.
>>
>> a[1] = [0,0]
>> b
>>
>> [0, [0, 0]]
>>
>
>
>  My python gets a different result:
>>
>
> So does mine, and I suspect so does denis' (spir).
> I think he made a mistake with the second assignment to a.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos


Glad to hear it.  That is what I was hoping, but I did not want to question
a helpful person.

-Denis H.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-28 Thread spir

On 01/29/2014 02:10 AM, Dave Angel wrote:

  Denis Heidtmann  Wrote in message:




  What is going on?  I am more confused than I was a week ago.


Simple. spir has copy/paste editing errors.


Oops! sorry

d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-28 Thread Alan Gauld

On 28/01/14 19:00, Denis Heidtmann wrote:

On Tue, Jan 28, 2014 at 12:28 AM, spir 

This is getting confusing with two times Denis!


> wrote:

a = [1,[2,3]]


I think the above line is a mistake. If Denis(spir) had missed
this out his code would work as he describes, but with it he
reassigns 'a' to a new list and, as you say breaks the connection.

If you forget about that line and keep a pointing at the original 
[1,[1,2]] list then the following code makes sense.



a[0] = 0
b

[0, [1, 2]]  # this is where I get lost.

a[1] = [0,0]
b

[0, [0, 0]]




My python gets a different result:


So does mine, and I suspect so does denis' (spir).
I think he made a mistake with the second assignment to a.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-28 Thread Dave Angel
 Denis Heidtmann  Wrote in message:
>
> 
 What is going on?  I am more confused than I was a week ago.


Simple. spir has copy/paste editing errors. 

-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-28 Thread Danny Yoo
Hi Denis,

Ok, stop for a moment.

Visit:http://pythontutor.com/visualize.html

Specifically, here's your program:


http://pythontutor.com/visualize.html#code=a%3D%5B1,%5B1,2%5D%5D%0Ab%3Da%0Aa%3D%5B1,%5B2,3%5D%5D%0Aa%5B0%5D%3D0%0Aa%5B1%5D%3D%5B0,0%5D&mode=display&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=2&curInstr=3


Step through it, and see if anything there makes sense.  :P

Try a few more simple programs there, including the examples discussed
earlier on this thread.

I have a feeling that your mental model of what's happening is not
quite matching the machine, so the visualizer at
http://pythontutor.com/visualize.html may help bridge that gap.



Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-28 Thread Denis Heidtmann
On Tue, Jan 28, 2014 at 12:28 AM, spir  wrote:

> 
>
>  a = [1, [1,2]]
 b = a
 b

>>> [1, [1, 2]]
>
>> b is a

>>> True
>
> a's and b's values are a single, unique object... as long as I only
> modified them (the values) partly:
>
>  a = [1,[2,3]]
 a[0] = 0
 b

>>> [0, [1, 2]]  # this is where I get lost.
>
>> a[1] = [0,0]
 b

>>> [0, [0, 0]]
>
>> a is b

>>> True
>
>  a[0] = b[0]
 a[0] is b[0]

>>> True
> 
>
> denis



My python gets a different result:

 >>> a=[1,[1,2]]
>>> b=a
>>> b
[1, [1, 2]]
>>> a=[1,[2,3]]  # this breaks the connection.
>>> a[0]=0
>>> b
[1, [1, 2]]
>>> a[1]=[0,0]
>>> b
[1, [1, 2]]
>>>

What is going on?  I am more confused than I was a week ago.

-Denis H
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-28 Thread spir

On 01/27/2014 06:04 PM, Denis Heidtmann wrote:

Apparently a[0]=b[0] does not qualify as "symbolic assignment" in this
case. a[0] is not a reference to b[0].  I think I see the essential
distinction.  Experience will complete the picture for me.


"symolic assignment" is my term, so whatever I mean with it qualifies as 
symbolic assignment ;-); and tes, your example a[0]=b[0] is indeed a symbolic 
assignment (because the right side "b[0]" denotes a value, an object, and could 
be on the left side of an assignment)
the distinction between "replacement" & "modification" also uses my terms; 
better you know that because if you talk with programmers using these terms as 
key words, others will be surprised


The real point is: maybe read again my previous post. I insisted on the fact 
that in python _symbols_ are not corelated, never ever. Your remark here seems 
to show that you expect a[0] and b[0] to be corelated, in such a way that if we 
change one of them the second should follow. No. Values are made unique by 
symbolic assignment; but this can only show if you modify them partly (not 
replace globally), thus can only show if those are complex values.



a = [1, [1,2]]
b = a
b

[1, [1, 2]]

b is a

True

a's and b's values are a single, unique object... as long as I only modifie them 
(the values) partly:



a = [1,[2,3]]
a[0] = 0
b

[0, [1, 2]]

a[1] = [0,0]
b

[0, [0, 0]]

a is b

True


a[0] = b[0]
a[0] is b[0]

True

Here i make their first elements the same unique value. But I'm blocked to show 
it (other than using 'is') because I cannot modify such objects (they are simple 
numbers). Since it is the _values_ which are related, not the symbols, if I 
replace one of them I break the relation.



a[0] = 9
a

[9, [2, 3]]

b

[1, [0, 0]]

Right? On the other, if I create a relatoin between their second elements, then 
I can have something more interesting:



a[1] = b[1]
a[1] is b[1]

True

a, b

([9, [0, 0]], [1, [0, 0]])

a[1][1] = 9
b[1][1]

9

a, b

([9, [0, 9]], [1, [0, 9]])

You will get used to it... Also, it just works most of the time, except for 
corner cases where you will be trapped 2-3 times until you get it. (The reason 
is that unconsciously, when we want several symbols to have their proper values, 
we just do it right by assigning them apart, even if they (initially) have the 
same values. When instead we want a symbol to refer to the same value as 
another, we correctly use a symbolic assignment; instead of incorrectly 
assigning an equal, but distinct, value.]


denis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread eryksun
On Mon, Jan 27, 2014 at 9:07 AM, Mark Lawrence  wrote:
> On 27/01/2014 09:53, spir wrote:
>>
>> Note: your example is strongly obscured by using weird and rare features
>> that don't bring any helpful point to the actual problematic concepts
>> you apparently want to deal with.
>>
>
> Nothing weird and rare about it, just something from the numpy maths library
> and not pure Python.

NumPy arrays may seem weird to someone who expects a slice to create a
shallow copy of the data, in the way that slicing a `list` creates a
shallow copy:

>>> a = [0, 2, 4]
>>> b = a[:]

`b` is a copy of list `a`, so modifying `b` has no effect on `a`:

>>> b[:] = [1, 3, 5]
>>> a
[0, 2, 4]

Slicing a NumPy array returns a new view on the data:

a = np.array([0, 2, 4], dtype=object)
b = a[:]

>>> b.base is a
True
>>> b.flags.owndata
False

The view shares the underlying data array, so modifying it also
changes the original:

>>> b[:] = [1, 3, 5]
>>> a
array([1, 3, 5], dtype=object)

You have to ask for a `copy`:

a = np.array([0, 2, 4], dtype=object)
b = a.copy()

>>> b.base is None
True
>>> b.flags.owndata
True

>>> b[:] = [1, 3, 5]
>>> a
array([0, 2, 4], dtype=object)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread eryksun
On Mon, Jan 27, 2014 at 2:01 PM, Danny Yoo  wrote:
> And variable binding itself can even have a slightly
> different meaning, depending on whether the surrounding context is a
> function definition or not, establishing a local or global variable
> binding.  Whew!

Name binding is local unless you tell Python otherwise by declaring a
name to be global (also nonlocal in 3.x). CPython, for example,
compiles top-level module code to use the STORE_NAME instruction,
which stores to the locals mapping of the current frame. If the name
is declared global, it instead uses STORE_GLOBAL.

Module-level code is flagged to create a frame for which locals and
globals are the same dict. You could, however, exec code using
separate dicts for locals and globals:

src = r'''
global x
x = 0
y = 1
'''

gvars, lvars = {}, {}
exec src in gvars, lvars



>>> sorted(gvars)
['__builtins__', 'x']

>>> sorted(lvars)
['y']

CPython bytecode:

>>> code = compile(src, '', 'exec')
>>> dis.dis(code)
  3   0 LOAD_CONST   0 (0)
  3 STORE_GLOBAL 0 (x)

  4   6 LOAD_CONST   1 (1)
  9 STORE_NAME   1 (y)
 12 LOAD_CONST   2 (None)
 15 RETURN_VALUE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Danny Yoo
> Apparently a[0]=b[0] does not qualify as "symbolic assignment" in this case.
> a[0] is not a reference to b[0].  I think I see the essential distinction.
> Experience will complete the picture for me.

Yes.  The distinction is something that is blurred by Python's syntax.
 The "=" is a conceptually different thing, based on what's on the
"left hand side" of the "=".  It can means "variable binding" or
"structure mutation", and those concepts are similar, but not the same
thing.   And variable binding itself can even have a slightly
different meaning, depending on whether the surrounding context is a
function definition or not, establishing a local or global variable
binding.  Whew!

Assignment can be tricky.  It's at the heart of one of the things that
makes programming "hard": it is very much about change, about
dynamics, about having to reason what the world looks like "before"
and "after" a change.

(And hence why some customized programming languages for beginners
outright prohibit the assignment operator.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Denis Heidtmann
Thanks for the responses.

The distinction between replacement and modification seems to capture the
essential aspect and helps to clarify the issue for me.

spir:
Quite the opposite, in python "symbolic assignment" (where the right side
also is a symbol) never copies, in fact never creates a new value, but bind
the left symbol to the same, unique value, as the right symbol.

If this is accurate, I can see crazy bugs in my future until I internalize
it.

Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[2,3]
>>> b=[10,20]
>>> a[0]=b[0]
>>> a
[10, 3]
>>> b[0]=100
>>> a
[10, 3]

Apparently a[0]=b[0] does not qualify as "symbolic assignment" in this
case. a[0] is not a reference to b[0].  I think I see the essential
distinction.  Experience will complete the picture for me.

-DH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Mark Lawrence

On 27/01/2014 09:53, spir wrote:

On 01/27/2014 07:16 AM, Denis Heidtmann wrote:

Running python 2.7 in linux

Below are two extremes.  Can I get some guidance on this?

Thanks,
-Denis H


a=zeros((2,3),dtype=int)
b=a
a[:,0]=[1,2]
a

array([[1, 0, 0],
[2, 0, 0]])

b

array([[1, 0, 0],
[2, 0, 0]])

a=2
a

2

b

array([[1, 0, 0],
[2, 0, 0]])


Note: your example is strongly obscured by using weird and rare features
that don't bring any helpful point to the actual problematic concepts
you apparently want to deal with.



Nothing weird and rare about it, just something from the numpy maths 
library and not pure Python.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Peter Otten
Denis Heidtmann wrote:

> Running python 2.7 in linux
> 
> Below are two extremes.  Can I get some guidance on this?

 a=zeros((2,3),dtype=int)
 b=a
 a[:,0]=[1,2]
 a
> array([[1, 0, 0],
>[2, 0, 0]])
 b
> array([[1, 0, 0],
>[2, 0, 0]])
 a=2
 a
> 2
 b
> array([[1, 0, 0],
>[2, 0, 0]])

In short:

x = ...

binds a name, it never copies. On the other side

x[whatever] = ...
or
x.attr = ...

can run arbitrary code that may or may not alter the object bound to x 
inplace. You have to know the type of x to know what happens.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread spir

On 01/27/2014 07:16 AM, Denis Heidtmann wrote:

Running python 2.7 in linux

Below are two extremes.  Can I get some guidance on this?

Thanks,
-Denis H


a=zeros((2,3),dtype=int)
b=a
a[:,0]=[1,2]
a

array([[1, 0, 0],
[2, 0, 0]])

b

array([[1, 0, 0],
[2, 0, 0]])

a=2
a

2

b

array([[1, 0, 0],
[2, 0, 0]])


Note: your example is strongly obscured by using weird and rare features that 
don't bring any helpful point to the actual problematic concepts you apparently 
want to deal with.


It seems you are confusing 2 issues: relation (reference) between values 
(objects) and relations between symbols (variables).


The last part of your example implies that you expect that, maybe, symbol 'b' 
may now magically point to 2 just because it were corelated with 'a', which was 
set to point to 2. Correct? If so, you are wrong: there is no relation between 
symbols in python (nore in any other language I know, for the matter). Symbols 
'a' and 'b' are independant, whatever the possible relations between their 
values. If you *replace* a's value, this has no effect on b, even if they 
previously held the very same, unique, value.


Python 3.3.2+ (default, Oct  9 2013, 14:50:09)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.

a = [1,2,3]
b = a
a is b

True

a = (1,2)   # replacement
b

[1, 2, 3]

Now, there are 2 ways to change a symbol's value: to *replace* it globally as 
above, or to *modify* it partly



a = [1,2,3]
b = a
a is b

True

a[1] = 0# modification
a

[1, 0, 3]

b

[1, 0, 3]

a is b

True

A misleading point is exemplified by "a is b": it does not mean that both 
symbols actually are the same one (unique), but that _their value objects_ are 
the same one (unique). This is the role of symbols: once defined, they are used 
for whatever they represent. Here symbols a & b just play their normal role of 
symbols, right?


The above example of modification is only possible if the value is complex (and 
mutable). Simple values like numbers or strings obviously cannot be modified, 
only replaced. Thus, such simple values just behave like "plain old values" in 
static or historical languages (in which there aren't symbols & values are 
runtime, instead plain adresses & raw data). In such languages there is 
systematic copy on assignment, unless one explicitely uses pointers. Maybe you 
are used to that, and expect such behaviour in python.


Quite the opposite, in python "symbolic assignment" (where the right side also 
is a symbol) never copies, in fact never creates a new value, but bind the left 
symbol to the same, unique value, as the right symbol.


Note: such are the semantics (the meaning) of the language. But since as said 
above this does not make any difference in practice for simple values, the 
language implementation is in fact free to copy under the hood, if this is 
simpler or more efficient to do so: the language's semantics are preserved 
nevertheless. However, python's standard implementation does not appear to do so:



a = -12.345
b = a
a is b

True# a & b are bound to the same value, there's only one -12.345

d





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Steven D'Aprano
Hi Denis, and welcome!

On Sun, Jan 26, 2014 at 10:16:25PM -0800, Denis Heidtmann wrote:

> Running python 2.7 in linux
> 
> Below are two extremes.  Can I get some guidance on this?

In Python, = is ALWAYS an alias, never a copy, unless you explicitly do 
something to make a copy. For example, with dicts, there is a `copy` 
method:

newdict = olddict.copy()

But it's not always *obvious* that you're just aliasing the same object. 
For example, this is obvious:

py> a = []
py> b = a  # b is now a new name for a
py> a.append(42)  # modifies the list in place
py> b
[42]

but this is not:

py> a = 23
py> b = a  # b is now a new name for a
py> a = a + 1
py> b
23


In this second case, b is unchanged because `a = a + 1` doesn't *modify* 
a, it makes a new value (23 + 1 => 24) and assigns that new value to a, 
leaving b unchanged.


In your example below, I take it you are using numpy. You should say so, 
in case it is unclear.

> >>> a=zeros((2,3),dtype=int)
> >>> b=a
> >>> a[:,0]=[1,2]
> >>> a
> array([[1, 0, 0],
>[2, 0, 0]])
> >>> b
> array([[1, 0, 0],
>[2, 0, 0]])

In this example, you bind the name "a" to an array of zeroes, then bind 
the name "b" to the same array. "Name binding" is another term for 
assignment in Python. So a and b are two names for the same array. When 
you modify the array via the name "a", b sees the same changes because 
they are the same array.

Even though the line

a[:,0] = [1, 2]

looks like an assignment, it's not actually a name binding, because 
you're only assigning to a slice of the array, not the name. That makes 
it an in-place modification, just like appending to a list.

But this code that follows:

> >>> a=2
> >>> a
> 2
> >>> b
> array([[1, 0, 0],
>[2, 0, 0]])

is different. Here, the line `a = 2` is an actual name binding. It makes 
the name `a` refer to the value 2, instead of the array it used to refer 
to. But that doesn't change the array in any way, nor does it change 
`b`, so b is unaffected.


Hope this helps,


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor