python noob here and playing around with the limitations of Tuples and Lists.
I don't have a problem, just a general query about usage of the __methodname__
methods in the Tuple class and/or in general.
I am aware that you cannot modify Tuples and in order to do so, you have to convert it to a list, modify said list, then convert back to Tuple but I had a play around with the __add__
method and found that it works. What are issues and limitations with using this to create new Tuples with modifications to an existing one?
CODE:
myTuple = ('item1', 2, 'item3', ['list1', 'list2'])
tupleModification = myTuple.__add__(('newTupleItem1','newTupleItem2'))
This outputs the following:
('item1', 2, 'item3', ['list1', 'list2'], 'newTupleItem1', 'newTupleItem2')
which is correct but i'm wondering if i'm playing with fire because I haven't seen this solution posted anywhere in relation to modifying Tuples.
EDIT: I am aware that you cannot modify existing Tuples and that this will create a new instance of one. I think I may have confused people with my naming conventions.
__add__
is the method which is called when you do:
myTuple + ("newTupleItem1", "newTupleItem2")
So this does not modify myTuple
but creates a new tuple
whose content is the content of myTuple
concatenated with ("newTupleItem1", "newTupleItem2")
.
You can print myTuple
to see that it has not been modified:
>>> myTuple
('item1', 2, 'item3', ['list1', 'list2'])
And you can check that myTuple
and tupleModification
are not the same object:
>>> myTuple is tupleModification
False
You cannot modify tuples, that's right. However, you can concatenate two existing tuples into a new tuple. This is done with the +
operator, which in turn calls the __add__
method. The resulting tuple will not be a "modification" of any of the original ones, but a new distinct tuple. This is what the code you posted does. More concisely, you can just do:
myTuple = ('item1', 2, 'item3', ['list1', 'list2'])
tupleModification = myTuple + ('newTupleItem1','newTupleItem2')
print(tupleModification)
# ('item1', 2, 'item3', ['list1', 'list2'], 'newTupleItem1', 'newTupleItem2')
EDIT: Just as a clarification, you cannot "edit" a tuple anyhow, that is, add or remove elements from it, or change its contents. However, if your tuple contains a mutable object, such as a list, then that inner object can be modified:
myTuple = (1, [2, 3])
myTuple[1].append(4)
print(myTuple)
# (1, [2, 3, 4])
I think fundamentally you're confused about the difference between creating a new object with modifications ( __add__
in this case) and modifying an existing object ( extend
for example).
__add__
As other answers already mentioned, the __add__
method implements the +
operator, and it returns a new object. Tuples and lists each have one. For example:
>>> tuple_0 = (1,)
>>> tuple_1 = tuple_0.__add__((2,))
>>> tuple_1 is tuple_0
False
>>>
>>> list_0 = [1]
>>> list_1 = list_0.__add__([2])
>>> list_1 is list_0
False
extend
Lists, which are mutable, have an extend
method, which modifies the existing object and returns None
. Tuples, which are immutable, don't. For example:
>>> list_2 = [4, 5, 6]
>>> id_save = id(list_2)
>>> list_2.extend([7])
>>> id(list_2) == id_save
True
>>>
>>> tuple_2 = (4, 5, 6)
>>> tuple_2.extend([7])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'extend'
Lists have other methods which change the existing object, like append
, sort
, pop
, etc, but extend
is the most similar to __add__
.
There is a difference between 1) modifying an existing tuple (in-place), and 2) leaving the original alone, but creating a new modified copy. These two different paradigms can be seen throughout computer programming, not just python tuples. For example, consider increment a number by one. You could modify the original, or you could leave the original alone, create a copy and then modify the copy.
MODIFYING IN-PLACE
BEFORE:
x == 5
AFTER:
x == 6
CREATING A MODIFIED COPY
BEFORE:
x == 5
AFTER:
x == 5 (unchanged)
y == 6
tuple.__add__
concatenates tuples. For example, (x, y) + (a, b, c)
returns (x, y, a, b, c)
tuple.__add__
does not modify the original tuples. It leaves the original tuples alone, and creates a new tuple which is the concatenation of the original two. This is contrast to something like list.append
or list.extend
which modifies the original list instead of returning a modified copy.
Tuple methods generally do the following:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.