473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Are tuple really immutable?

Hi

Consider the following tuples:
t = ([1],[2])
u = (1,2)
v = ('1','2')
t ([1], [2]) u (1, 2) v ('1', '2') t.__hash__() Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable u.__hash__() 219750523 v.__hash__() -1786881095 d = dict()
d[t] = 't' Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable d[u] = 'u'
d[v] = 'v'
d {('1', '2'): 'v', (1, 2): 'u'}

t, u and v are all tuples. t and v elements are sequences.
Yet, t cannot be a dictionnary key because its elements are mutable.

1) Given a tuple, how can I know if it can be a dictionnary key or not?

Of course I could call __hash__ and catch for a TypeError exception,
but I'm looking for a better way to do it.

2) Would it be possible to have a "ismutable" function or method? Like: t.ismutable() True, well maybe not... u.ismutable() False u.ismutable() False

3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but: t[0].append(0)
t ([1, 0], [2])

The tuple is immutable but its elements can be mutable: I tend to think
that it means that the tuple is mutable. Indeed, it changed!

4) Even more confusing: I had the following strange result:
(with both Python 2.3.3 and 2.4) t[0]+=[1] Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment t

([1, 0, 1], [2])
There was an exception, but the list was still changed!?

Chris

Jul 18 '05 #1
5 1668

"Chris" <ch**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:
t[0].append(0)
t ([1, 0], [2])

The tuple is immutable but its elements can be mutable: I tend to think
that it means that the tuple is mutable. Indeed, it changed!
Well, it did and it didn't. If you went in and did an
id() on each element, you'd discover that the actual
objects didn't change: the exact same ids were in
the exact same slots before and after. What you're
seeing is the way the str() and repr() functions print
the values of embedded objects, not the ids.

4) Even more confusing: I had the following strange result:
(with both Python 2.3.3 and 2.4) t[0]+=[1] Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment t
([1, 0, 1], [2])
There was an exception, but the list was still changed!?


I consider that an error, but I've had several people
tell me that it's the way it's supposed to be.
I simply quit trying to get the point across.
As far as I'm concerned, the behavior is simply
wrong. Since mutating the object didn't change
the id, it shouldn't try to replace the object in
the tuple.

John Roth
Chris


Jul 18 '05 #2
"Chris" wrote:
1) Given a tuple, how can I know if it can be a dictionnary key or not?

Of course I could call __hash__ and catch for a TypeError exception,
but I'm looking for a better way to do it.
calling hash(obj) is the only sane way to figure out if calling hash(obj)
will work.
2) Would it be possible to have a "ismutable" function or method?
objects are mutable only if they have some method (visible or __hidden__)
that can be used to modify their contents. from the Python runtime perspec-
tive, objects are objects.
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:
t[0].append(0)
t ([1, 0], [2])

The tuple is immutable but its elements can be mutable: I tend to think
that it means that the tuple is mutable. Indeed, it changed!
as map(id, t) can tell you, the tuple object itself wasn't modified.
4) Even more confusing: I had the following strange result:
(with both Python 2.3.3 and 2.4) t[0]+=[1] Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment t

([1, 0, 1], [2])
There was an exception, but the list was still changed!?


that's a silly side-effect of how "+=" is defined for lists; the "+" part of the
expression works, and modifies the target object, but the "=" part fails.

</F>

Jul 18 '05 #3
Chris <ch**********@gmail.com> wrote:
...
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:
t[0].append(0)
t

([1, 0], [2])

The tuple is immutable but its elements can be mutable: I tend to think
that it means that the tuple is mutable. Indeed, it changed!


Maybe the last page of
<http://mail.python.org/pipermail/python-list/2002-April/099227.html>
can help with this conceptual issue.
Alex
Jul 18 '05 #4
Chris <ch**********@gmail.com> wrote:
...
3) In this example, is t considered mutable or not?
"Tuple are immutable" says the doc, but:
>>> t[0].append(0)
>>> t

([1, 0], [2])

The tuple is immutable but its elements can be mutable: I tend to think
that it means that the tuple is mutable. Indeed, it changed!


No, not in the way intended by the word 'mutable'. A tuple is like an
ordered club roster written in indelible ink before the time of whiteout.
The members of the club may change (change jobs, residence, relationships,
etc) but the roster remains the same: same members, same ranking.

Terry J. Reedy

Jul 18 '05 #5
I'm suing Google Groups (beta) which has a treeview and my thanks were
a reply to Fredrik Lundh. In fact I simply clicked a "reply" link below
his post.
Of course you all helped me to better understand the
"mutable/immutable" concept but Fredrik Lundh deserves more thanks
since he replied to all my questions ;-)

Chris

Jul 18 '05 #6

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

Similar topics

10
by: Carlo v. Dango | last post by:
Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new...
9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
50
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,...
37
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
77
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
7
by: Shafik | last post by:
Hello folks, I am an experienced programmer, but very new to python (2 days). I wanted to ask: what exactly is the difference between a tuple and a list? I'm sure there are some, but I can't...
2
by: hall.jeff | last post by:
Before the inevitable response comes, let me assure you I've read through the posts from Guido about this. 7 years ago Guido clearly expressed a displeasure with allowing these methods for tuple....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.