473,569 Members | 2,763 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 3378
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
3658
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
1702
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: +---+---+---+---+---+...
5
3831
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...
3
1166
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
7703
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...
0
7619
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...
0
8139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7984
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...
0
6290
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...
1
5515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3663
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...
1
2119
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
0
954
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.