Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
python question - copying a list or dictionary
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rock Doctor  
View profile  
 More options Apr 26 2010, 1:11 pm
From: Rock Doctor <usdans...@gmail.com>
Date: Mon, 26 Apr 2010 10:11:26 -0700 (PDT)
Local: Mon, Apr 26 2010 1:11 pm
Subject: [NORLUG] python question - copying a list or dictionary
Simple question: if my_thing is a dictionary or list, will
  x = my_thing*1
create a copy (rather than a reference)?

I'm aware of other methods of copying my_thing; just want to know if
the above is legit.

--
For archives and more options, visit this group at http://groups-beta.google.com/group/norlug
To unsubscribe from this group, send email to norlug-unsubscribe@googlegroups.com

Subscription settings: http://groups.google.com/group/norlug/subscribe?hl=en


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Gurno  
View profile  
 More options Apr 26 2010, 1:31 pm
From: Adam Gurno <adam.gu...@gmail.com>
Date: Mon, 26 Apr 2010 12:31:41 -0500
Local: Mon, Apr 26 2010 1:31 pm
Subject: Re: [NORLUG] python question - copying a list or dictionary

Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> my_thing = []
>>> x = my_thing * 1
>>> print x is my_thing, x, my_thing
False [] []
>>> y = 5
>>> mything = [44, "word", y]
>>> x = mything * 1
>>> print x is mything, x, mything

False [44, 'word', 5] [44, 'word', 5]
>>> y = 6
>>> print x is mything, x, mything

False [44, 'word', 5] [44, 'word', 5]

Copies copies everwhere, apparently.

P.S. what you want is "is"

P.P.S. Offer may be void in the newest version of Python.

A

--
Adam Gurno
a...@gurno.com

--
For archives and more options, visit this group at http://groups-beta.google.com/group/norlug
To unsubscribe from this group, send email to norlug-unsubscribe@googlegroups.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rock Doctor  
View profile  
 More options Apr 26 2010, 3:32 pm
From: Rock Doctor <usdans...@gmail.com>
Date: Mon, 26 Apr 2010 12:32:04 -0700 (PDT)
Local: Mon, Apr 26 2010 3:32 pm
Subject: [NORLUG] Re: python question - copying a list or dictionary
I knew about "is" but didn't think to use it. As for dictionaries, no
"*" operation, so that answers that part of my question .

--
For archives and more options, visit this group at http://groups-beta.google.com/group/norlug
To unsubscribe from this group, send email to norlug-unsubscribe@googlegroups.com

Subscription settings: http://groups.google.com/group/norlug/subscribe?hl=en


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Gurno  
View profile  
 More options Apr 26 2010, 3:42 pm
From: Adam Gurno <adam.gu...@gmail.com>
Date: Mon, 26 Apr 2010 14:42:20 -0500
Local: Mon, Apr 26 2010 3:42 pm
Subject: Re: [NORLUG] Re: python question - copying a list or dictionary

OTOH, is works strangely on immutable types:

Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> mything=55
>>> x=mything * 1
>>> print x is mything, x, mything
True 55 55
>>> x=mything * 2
>>> print x is mything, x, mything

False 110 55

*shrug*

A

On Mon, Apr 26, 2010 at 2:32 PM, Rock Doctor <usdans...@gmail.com> wrote:
> I knew about "is" but didn't think to use it. As for dictionaries, no
> "*" operation, so that answers that part of my question .

> --
> For archives and more options, visit this group at
> http://groups-beta.google.com/group/norlug
> To unsubscribe from this group, send email to
> norlug-unsubscribe@googlegroups.com

> Subscription settings:
> http://groups.google.com/group/norlug/subscribe?hl=en

--
Adam Gurno
a...@gurno.com

--
For archives and more options, visit this group at http://groups-beta.google.com/group/norlug
To unsubscribe from this group, send email to norlug-unsubscribe@googlegroups.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Ondich  
View profile  
 More options Apr 26 2010, 5:27 pm
From: Jeff Ondich <jond...@carleton.edu>
Date: Mon, 26 Apr 2010 16:27:10 -0500
Local: Mon, Apr 26 2010 5:27 pm
Subject: Re: [NORLUG] Re: python question - copying a list or dictionary
That strangeness has to do with how small integers are implemented.
Every reference to a small integer object (55 is apparently small enough
to be "small") is actually pointing to the same instance.  Larger
numbers (e.g. 5555) act more like mutable types.

Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> mything=55
 >>> x = mything * 1
 >>> print x is mything, x, mything
True 55 55
 >>> mything = 5555
 >>> x = mything * 1
 >>> print x is mything, x, mything
False 5555 5555
 >>>

It turns out that the breaking point is between 256 and 257:

 >>> for k in range(1000):
...     x = k * 1
...     print x is k, x, k
...
True 0 0
True 1 1
True 2 2
True 3 3
True 4 4
...
True 253 253
True 254 254
True 255 255
True 256 256
False 257 257
False 258 258
False 259 259
...

While we're having fun, have you guys every tried these Python statements?

   True = False
   int = float

and then play around for a while?

Jeff

--
For archives and more options, visit this group at http://groups-beta.google.com/group/norlug
To unsubscribe from this group, send email to norlug-unsubscribe@googlegroups.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Usdansky  
View profile   Translate to Translated (View Original)
 More options Apr 27 2010, 9:41 am
From: Steven Usdansky <usdans...@gmail.com>
Date: Tue, 27 Apr 2010 08:41:59 -0500
Local: Tues, Apr 27 2010 9:41 am
Subject: Re: [NORLUG] Re: python question - copying a list or dictionary

Python 2.6.4 (r264:75706, Apr  1 2010, 02:55:51)
[GCC 4.4.3 20100226 (Red Hat 4.4.3-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> x=("Four","score","and","seven")
>>> y=x*1
>>> x is y

True

Is immutability the key?
Small numbers are immutable, but large numbers are not??

--
For archives and more options, visit this group at http://groups-beta.google.com/group/norlug
To unsubscribe from this group, send email to norlug-unsubscribe@googlegroups.com

Subscription settings: http://groups.google.com/group/norlug/subscribe?hl=en


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Bullock  
View profile  
 More options Apr 27 2010, 11:48 am
From: Kevin Bullock <kbull...@ringworld.org>
Date: Tue, 27 Apr 2010 10:48:26 -0500
Local: Tues, Apr 27 2010 11:48 am
Subject: Re: [NORLUG] Re: python question - copying a list or dictionary
On 27 Apr 2010, at 8:41 AM, Steven Usdansky wrote:

> Python 2.6.4 (r264:75706, Apr  1 2010, 02:55:51)
> [GCC 4.4.3 20100226 (Red Hat 4.4.3-8)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> x=("Four","score","and","seven")
> >>> y=x*1
> >>> x is y
> True

> Is immutability the key?
> Small numbers are immutable, but large numbers are not??

I suspect it's not so well-defined as "immutable types aren't copied." I think you have to investigate it on a type-by-type basis. Since tuples get used for things like packing function args and other internal uses (right?), it makes sense to optimize them to never be duplicated.

Small numbers and large numbers are both immutable, though: any operation on a number returns a new number. So large numbers are immutable, but get copied (for the reason Jeff described). Ruby and Lisp do a similar optimization. In Ruby, a variable slot is 32 bits, the high bit of which indicates whether it stores a Fixnum or an object reference. So you get 31-bit Fixnums, and Integers above that automatically become Bignums. Python appears to store small *positive* integers up to one byte:

Python 2.6 (r26:66714, Feb 27 2009, 11:44:36)
[GCC 4.0.1 (Apple Inc. build 5488)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> x = -55
>>> y = x*1
>>> [x,y, x is y]

[-55, -55, False]

but anything above that is a reference. That's interesting because Python guarantees that `int`s are at least 32 bits (signed). So not all `int`s are optimized to be stored without a reference(?).

pacem in terris / mir / shanti / salaam / heiwa
Kevin R. Bullock

--
For archives and more options, visit this group at http://groups-beta.google.com/group/norlug
To unsubscribe from this group, send email to norlug-unsubscribe@googlegroups.com

Subscription settings: http://groups.google.com/group/norlug/subscribe?hl=en


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »