Let's talk about the problems encountered here. First, we can't figure out whether to transfer references or objects at the beginning. (I don't know if it is reasonable to say this, but from the perspective of c ++, I think it will be simpler.) I have a list of flights in the airline category, so if I append a flight object to this list, and the next flight object changes, will the one in the corresponding company list change? The same question also appears in the seat list of the flight object and the seat list of the customer. later, I tried it and found that it would change. This is the transfer of reference. in simple python, we actually say:
When you create an object and assign it a variable, this variable is onlyReferenceThe object instead of the object itself! That is to say, the variable name points to the memory of the object stored in your computer. This is called the name-to-ObjectBind.
In fact, the address of the object is stored in the array. I think this is the same as that in java, except for the basic data type. For example:
A = 0
B =
A = 9
In this case, how much B is? Will B change with the change of? The answer is no.
Another example is:
A = 'A'
B =
A = 'B'
However, if you write:
Class:
Def _ init _ (self ):
Self. name =''
A = ()
B =
A. name = "nihao"
Print B. name
The result is nihao.
This indicates that non-basic types are passed references. Another example is a program,
Fun ():
A = 0
A = 9
Fun ()
Print
The answer is "9". This indicates that python is consistent with java in this respect.
The second problem is the code structure problem. I abstracted it into the following form:
Class:
Def _ init _ (self, mlist = []):
Self. mylist = mlist
Class B:
Def _ init _ (self ):
Self. P = None
Def setList (self, ):
Self. P =
A. mylist. append (self)
A1 = ()
A2 = ()
B1 = B ()
B2 = B ()
B1.setList (a1)
B2.setList (a1)
Print a1.mylist
Print a2.mylist
The printed results will surprise me ..... so far I have not figured out why ......... you can run it yourself ....... if you know this, make sure you comment and tell me about it !!!!! Kneeling !!!!