Re: [Tutor] assignment statements in python

2006-06-12 Thread Python
On Mon, 2006-06-12 at 09:37 -0400, Kermit Rose wrote:
>   
> From: Python 
> Date: 06/11/06 22:59:38 
> To: Kermit Rose 
> Cc: Tutor Python 
> Subject: Re: [Tutor] assignment statements in python 
>  
> 
> The basic python objects: numbers, strings, and tuples are immutable and 
> can not be changed (mutated). 
> a = B = 3 
> B = 4 # binds B to a different object with value 4 
> # the object with value 3 is unchanged 
> print a 
> 3 
>  
> **
> If I write 
>  
> a = 3
> a = 4
> a = 5
>  
> Are the objects containing 3 and 4 erased when they no longer have a name?

Yes

>  
> **
>  
> >>>>>
>  
> Container objects such as lists and dictionaries can be changed in 
> place. 
> >>> a = B = [1,2,3] 
> >>> B.append(4) # changes (mutates) B 
> >>> print a 
> [1, 2, 3, 4] 
>  
> **
>  
> Good.  Now I know a more efficient way to extend  an array.  
>  
> I had been creating an entire new array, and equivalencing the old array to
> it.
>  
> **
> >>>>>>
>  
> >>> B[2] = B[3] # positions 2 and 3 reference the same object 
> >>> print B 
> [1, 2, 4, 4] 
> >>> print a 
> [1, 2, 4, 4] 
>  
> **
>  
> I still don't know how to make it so that
>  
> If  B = [ 1,2,4,5]
>  
> B.append(value of B[4])

There is no B[4]

B[0] is 1
B[1] is 2
B[2] is 4
B[3] is 5

Perhaps you mean to search B looking for the value 4 and then append
that value?

index_of_4 = B.index(4) # returns index to location of first 4
B.append( B[index_of_4])# appends the 4 to the end of B

> copy the value of B[2] into B[3]
>>> import copy
>>> B = [ 1,2,4,5]
>>> B[3] = copy.copy(B[2])
>>> B
[1, 2, 4, 4]

Since 4 is immutable, there is no need to use the copy module, but it is
there for when you need to make copies of an object.

> copy  the value 3 into B[2].

B[2] = 3# no need for a copy since 3 is immutable

>  
> 
> Or,  equivalently,
>  
> If B = [1,2,4,5]
>  
> Insert the value 3 between
> B[1] and b[2],
>  
>>> B = [ 1,2,4,5]
>>> B.insert(2,3)   # inserts 3 before B[2]
>>> B
[1, 2, 3, 4, 5]

>>> help(B.insert)

insert(...)
L.insert(index, object) -- insert object before index

(Use q to leave the help screen)
> so that B 
> becomes
> [1,2,3,4,5].
> > Kermit < [EMAIL PROTECTED] > 
> > 
> > 
> > 
> > ___ 
> > Tutor maillist - Tutor@python.org 
> > http://mail.python.org/mailman/listinfo/tutor 
-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] assignment statements in python

2006-06-12 Thread Danny Yoo
>> Assignment in Python is not a copy, it is a name binding. Assignment 
>> creates a name for an object. If you assign the same object to two 
>> names, they both are bound to the same thing. If the object is mutable, 
>> like a list, changes to the object will be seen regardless of which 
>> name you use to refer to it.
>>
>> **
>
> In that case, is it possible to copy a variable by value, instead of by 
> reference, in Python?

The 'copy' module is available,

 http://www.python.org/doc/lib/module-copy.html

So in a pinch, if we're really paranoid, we can pass copies of our 
argument values to a function.  In general, though, a function should 
really document if it mutates its arguments, because that's generally a 
very rude thing to do unless it's the expected behavior.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] assignment statements in python

2006-06-12 Thread Michael Sullivan
On Sun, 2006-06-11 at 22:14 -0400, Kermit Rose wrote:
>   Message: 1
> Date: Sun, 11 Jun 2006 06:58:39 -0400
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] buggy bug in my program
> Cc: tutor@python.org
>  
> Assignment in Python is not a copy, it is a name binding. Assignment
> creates a name for an object. If you assign the same object to two
> names, they both are bound to the same thing. If the object is mutable,
> like a list, changes to the object will be seen regardless of which name
> you use to refer to it.
>  
> **

In that case, is it possible to copy a variable by value, instead of by
reference, in Python?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] assignment statements in python

2006-06-12 Thread Kent Johnson
Kermit Rose wrote:
>   
> Message: 1
> Date: Sun, 11 Jun 2006 06:58:39 -0400
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] buggy bug in my program
> Cc: tutor@python.org
>  
> Assignment in Python is not a copy, it is a name binding. Assignment
> creates a name for an object. If you assign the same object to two
> names, they both are bound to the same thing. If the object is mutable,
> like a list, changes to the object will be seen regardless of which name
> you use to refer to it.
>  
> **
>  
> I feel a little bit better now that I know that there is a reason for what
> my
> program did.
>  
> However, I still don't have any idea how to copy values from one cell in 
> an array to the adjacent cell in the same array.

You need to copy the value stored in the list, which is itself a list.

> It must be possible, for otherwise, you could not sort an array.

Actually sorting doesn't require copying the values in the list, it just 
requires moving values to different locations of the list.

A list element is somewhat like a name - it is a reference to a value, 
not a container for a value.

If you say
a=[1,2,3]
b=a

then a and b refer to the same list. Likewise, if you say
x=[ [1,2,3], [4,5,6] ]
x[1] = x[0]

then x[1] and x[0] refer to the same list. If you want x[1] (or b) to 
refer to a new list, you have to copy the old list:
x[1] = x[0][:]

list[:] is the slice of the list that goes from the beginning to the end 
- a copy.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] assignment statements in python

2006-06-11 Thread Python
On Sun, 2006-06-11 at 22:14 -0400, Kermit Rose wrote:
>   Message: 1
> Date: Sun, 11 Jun 2006 06:58:39 -0400
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] buggy bug in my program
> Cc: tutor@python.org
>  
> Assignment in Python is not a copy, it is a name binding. Assignment
> creates a name for an object. If you assign the same object to two
> names, they both are bound to the same thing. If the object is mutable,
> like a list, changes to the object will be seen regardless of which name
> you use to refer to it.
>  
> **
>  
> I feel a little bit better now that I know that there is a reason for what
> my
> program did.
>  
> However, I still don't have any idea how to copy values from one cell in 
> an array to the adjacent cell in the same array.
>  
> I looked  at the reference ,
>  
> http://www.effbot.org/zone/python-objects.htm
>  
> that you gave,
>  
> but did not gleam any hint from it how to copy values from one place in an
> array to another place within the same array.
>  
> It must be possible, for otherwise, you could not sort an array.
>  
> 
> It is quite remarkable that my not knowing that 
>  
> assignment is not a copy 
>  
> gave me no difficulties before now.
The basic python objects: numbers, strings, and tuples are immutable and
can not be changed (mutated).
a = b = 3
b = 4   # binds b to a different object with value 4
# the object with value 3 is unchanged
print a
3

Container objects such as lists and dictionaries can be changed in
place.
>>> a = b = [1,2,3]
>>> b.append(4) # changes (mutates) b
>>> print a
[1, 2, 3, 4]
>>> b[2] = b[3] # positions 2 and 3 reference the same object
>>> print b
[1, 2, 4, 4]
>>> print a
[1, 2, 4, 4]


>  
> 
> 
> Kermit  <  [EMAIL PROTECTED]  >
>  
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor