Python meta-ancestor unable to nest problem

I recently studied Python and encountered some questions about meta-ancestors and lists, as follows:

Python version is 3.6

the code is as follows:

[1, 2] + [[3]] -sharp -> [1, 2, [3]]

(1, 2) + ((3),) -sharp (1, 2, 3),  (1, 2, (3))?
-sharp , 
(1, 2, (((3)))) -sharp -> (1, 2, 3)

(1, 2) + (3, ) -sharp -> (1, 2, 3)

there are two problems

  1. Why tuples can also be added. I understand that Yuanzu is an immutable object, so why can I add it?
  2. Why can"t you nest Yuanzu? As shown in the above example, all the results are transformed into "one-dimensional" tuples, and the principle is not well understood?

I would like to have an answer from my seniors. I would appreciate it.

Oct.20,2021

praise your problem description

first. < hr >
  1. Let's start with an example: integers and strings are also immutable, so why can we add them? In fact, whether two objects can be added in python only depends on the object's _ _ add__ method. As long as this method is implemented, it can be added
  2. .
  3. is not because you can't, but because your tuple after the plus sign is wrong. Because the literal amount of tuples is special, it is declared using () , and when there is only one element in a tuple, there is a semantic ambiguity with the use of parentheses in python syntax, so python stipulates that when a tuple has only one element, it must be followed by a comma to indicate that it is a tuple, not an expression wrapped in parentheses. Your second expression (1,2) + ((3),) ) should be written as (1,2) + ((3,),) )
Menu