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
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.
On Mon, Apr 26, 2010 at 12:11 PM, Rock Doctor <usdans...@gmail.com> wrote:
> 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
-- 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
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
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
-- 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
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?
Adam Gurno wrote:
> 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 > <mailto: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
> <mailto:norlug-unsubscribe@googlegroups.com>
> -- > Adam Gurno
> a...@gurno.com <mailto: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
-- 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
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
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