473,657 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tuple slices

Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that
tuples are immutable ? I ended up writing a custom ImmutableSequen ce class that does this, but I
wonder why it is not implemented for tuples.

George
Jul 18 '05 #1
29 3391
George Sakkis wrote:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that
tuples are immutable ?


really?
a = 1, 2, 3
b = a[:]
a is b

True

</F>

Jul 18 '05 #2
Fredrik Lundh wrote:
George Sakkis wrote:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that
tuples are immutable ?


really?

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

True


My impression was that full tuple copies didn't actually copy, but that
slicing a subset of a tuple might. Not exactly sure how to test this, but:

py> a = 1, 2, 3
py> a[:2] is a[:2]
False

So _something_ at least is different between the two slices...

Steve
Jul 18 '05 #3
On Mon, 24 Jan 2005 18:45:46 +0100
"Fredrik Lundh" <fr*****@python ware.com> wrote:
George Sakkis wrote:
Why does slicing a tuple returns a new tuple instead of a view of
the existing one, given that tuples are immutable ?
really?


Well... seems like this case is optimized to return the original tuple
just incrementing its reference count and returning

tupleobject.c, 330-335

if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckEx act(a)) {
Py_INCREF(a);
return (PyObject *)a;
}

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

True

</F>

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #4
On Mon, 24 Jan 2005 18:45:46 +0100
"Fredrik Lundh" <fr*****@python ware.com> wrote:
George Sakkis wrote:
Why does slicing a tuple returns a new tuple instead of a view of
the existing one, given that tuples are immutable ?
really?

Well... seems like this case (slicing the whole tuple) is optimized to
return the original tuple just incrementing its reference count and returning
tupleobject.c, 330-335

if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckEx act(a)) {
Py_INCREF(a);
return (PyObject *)a;
}

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

True

</F>

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #5
Steven Bethard wrote:
>a = 1, 2, 3
>b = a[:]
>a is b

True


My impression was that full tuple copies didn't actually copy, but that slicing a subset of a
tuple might. Not exactly sure how to test this, but:

py> a = 1, 2, 3
py> a[:2] is a[:2]
False


yup. and to figure out why things are done this way, consider this case:
a = give_me_a_huge_ tuple()
len(a) (a rather large number) b = a[:2]
del a


(IIRC, I proposed to add "substrings " when I implemented the Unicode string
type, but that idea was rejected, for the very same "and how do you get rid of
the original object" reason)

</F>

Jul 18 '05 #6
Fredrik Lundh wrote:
Steven Bethard wrote:

My impression was that full tuple copies didn't actually copy, but that slicing a subset of a
tuple might. Not exactly sure how to test this, but:

py> a = 1, 2, 3
py> a[:2] is a[:2]
False


yup. and to figure out why things are done this way, consider this case:
>>> a = give_me_a_huge_ tuple()
>>> len(a) (a rather large number) >>> b = a[:2]
>>> del a


(IIRC, I proposed to add "substrings " when I implemented the Unicode string
type, but that idea was rejected, for the very same "and how do you get rid of
the original object" reason)


Ahh. Yeah, that seems sensible. I don't think I've ever written code
like that, but if I did, I'd almost certainly want it to work as it does
now...

Steve
Jul 18 '05 #7
The cpython implementation stores tuples in memory like this:
[common fields for all Python objects]
[common fields for all variable-size python objects, including tuple size]
[fields specific to tuple objects, if any]
[array of PyObject*, one for each item in the tuple]
This way of storing variable-size Python objects was chosen in part
because it reuqires only one allocation for an object, not two.
However, there is no way for one tuple to point to a slice of another
tuple.

there's no reason that some other python implementation couldn't make a
different choice.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFB9V1/Jd01MZaTXX0RAnD MAJ0f2v26tba9j4 6KsYV3SkylB51Kl QCfeTtK
YYuTzz1nulvDc8A d9p78AGo=
=+SO0
-----END PGP SIGNATURE-----

Jul 18 '05 #8

"Fredrik Lundh" <fr*****@python ware.com> wrote in message
news:ma******** *************** *************** *@python.org...
Steven Bethard wrote:
>>a = 1, 2, 3
>>b = a[:]
>>a is b
True


My impression was that full tuple copies didn't actually copy, but that slicing a subset of a
tuple might. Not exactly sure how to test this, but:

py> a = 1, 2, 3
py> a[:2] is a[:2]
False


yup. and to figure out why things are done this way, consider this case:
>>> a = give_me_a_huge_ tuple()
>>> len(a) (a rather large number) >>> b = a[:2]
>>> del a


(IIRC, I proposed to add "substrings " when I implemented the Unicode string
type, but that idea was rejected, for the very same "and how do you get rid of
the original object" reason)

</F>


Fair enough. So perhaps the question is whether such cases are more regular than something like:
a = give_me_a_huge_ tuple()
slices = [a[i:j] for i in xrange(len(a)) for j in xrange(i+1, len(a)+1)]

George
Jul 18 '05 #9
George Sakkis wrote:
Fair enough. So perhaps the question is whether such cases are more regular than something like:
a = give_me_a_huge_ tuple()
slices = [a[i:j] for i in xrange(len(a)) for j in xrange(i+1, len(a)+1)]


I believe the general usage of tuples tends to mean that
"give_me_a_huge _tuple()" just doesn't happen except with
those who insist on using tuples as though they were nothing
other than read-only lists.

If you use a tuple the way it was apparently intended, you
are extraordinarily unlikely to find yourself with a
huge one requiring slicing in such a way that you care
whether it is a "view" or a new object.

-Peter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
3676
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this, I'm just curious why it is neccesary.. Thanks,
31
1713
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example: +---+---+---+---+---+ | H | e | l | p | A |
5
3832
by: NuberSteve | last post by:
I'm very new to using CSS and also the concept of slices for mouse-overs, and have made my first attempt at using ImageReady to generate slices of a world map. I basically wanted a map that would show various countries appearing to be depressed when "moused-over". To keep it simple at first, I just decided to try two countries. After copying the HTML and JavaScript codes generated by ImageReady into the page I wanted to insert the map into,...
3
1167
by: David | last post by:
That xpairs() generator is nice, but it's not the best possible code What do you mean by best possible? Most efficient? Most readable? And why don't you use islice? eg: def xpairs(seq): len_seq = len(seq) for i, e1 in enumerate(seq):
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.