1 2 question one: What will be the output of the following code? Say your answer and explain it. 3 classParent (object):4x = 15 classChild1 (Parent):6 Pass7 classChild2 (Parent):8 Pass9 Printparent.x, child1.x, child2.xTenchild1.x = 2 One Printparent.x, child1.x, child2.x AParent.x = 3 - Printparent.x, child1.x, child2.x - Answer the - the output of the above code is: - -1 1 1 +1 2 1 -3 2 3 +What makes you confused or surprised is that the output of the last line is 3 2 3 instead of 3 2 1. Why does changing the value of Parent.x also change the value of child2.x, but the child1.x value does not change at the same time? A at The key to this answer is that in Python, class variables are internally processed as dictionaries. If the name of a variable is not found in the dictionary of the current class, the ancestor class (such as the parent class) will be searched until the referenced variable name is found (if the referenced variable name is not found in its own class and not in the Ancestor class, a Attributeerror exception is thrown). - -Therefore, setting X = 1 in the parent class causes the class variable x to have a value of 1 in reference to the class and any of its subclasses. That's because the first onePrintThe output of the statement is 1 1 1. - -Then, if any of its subclasses rewrite the value (for example, we execute the statement child1.x = 2), then the value is changed only in the subclass. That's why the second onePrintThe output of the statement is 1 2 1. - inFinally, if the value is changed in the parent class (for example, we execute the statement parent.x = 3), the change affects any value in the subclass that does not override the value (the subclass that is affected in this example is Child2). That's why a thirdPrintOutput is 3 2 3. - to question two: What will be the output of the following code? Say your answer and explain it? + defdiv1 (x, y): - Print("%s/%s =%s"% (x, y, x/y)) the defdiv2 (x, y): * Print("%s//%s =%s"% (x, y, x//y)) $Div1 (5,2)Panax NotoginsengDiv1 (5.,2) -Div2 (5,2) theDiv2 (5.,2.) + Answer A theThis answer actually depends on whether you're using Python 2 or Python 3. + -In Python 3, the expected output is: $ $5/2 = 2.5 -5.0/2 = 2.5 -5//2 = 2 the5.0//2.0 = 2.0 -In Python 2, however, the output of the above code will be:Wuyi the5/2 = 2 -5.0/2 = 2.5 Wu5//2 = 2 -5.0//2.0 = 2.0 AboutBy default, if two operands are integers, Python 2 automatically performs an integer calculation. As a result, a value of 5/2 is 2, whereas the 5./2 value is ' 2.5". $ -Note that, however, you can reload this behavior in Python 2 (for example, to reach the Python 3 you want to), by adding the following import: - - from __future__ ImportDivision AIt is also important to note that the "Double dash" (//) operator will always perform an integer division, regardless of the type of operand, which is why the 5.0//2.0 value is 2.0. + theNote: In Python 3, the/operator is a floating-point division, and//is divisible (that is, the quotient has no remainder, such as 10//3 results are 3, the remainder is truncated, and (-7)//3 results are-3. This algorithm is not the same as many other programming languages, and it is important to note that their division of operations will take a value in the direction of 0. In Python 2,/is divisible, that is, and Python 3//operator is the same,) - $ question three: What will the following code output? theList = ['a','b','C','D','e'] the PrintList[10:] the Answer the - The above code will output [] and will not cause a indexerror. in theAs expected, attempting to access a member that exceeds the list index value will result in Indexerror (for example, access to the above list of list[10]). Nonetheless, attempts to access a list of slices that are out of list membership as the starting index will not cause indexerror, and will simply return an empty list. the About a nasty little problem is that it causes bugs and the problem is difficult to track because it does not raise errors at run time. the the question four: What will be the output of the following code? Say your answer and explain it? the defmultipliers (): + return[LambdaX:I * x forIinchRange (4)] - Print[M (2) forMinchmultipliers ()] the How will you modify the definition of multipliers to produce the desired resultBayi the Answer the -The output of the above code is [6, 6, 6, 6] (instead of [0, 2, 4, 6]). - theThe reason for this is that the late binding of the Python closure results in the late binding, which means that the variables in the closure are looked up when the internal function is called. So the result is that when any function returned by the multipliers () is called, at that time, the value of I is looked up in the scope around which it was called, and by that time, no matter which returned function is called, forThe loop is complete, and the last value of I is 3, so the value of each returned function multiplies is 3. So a value equal to 2 is passed over the code, and they return a value of 6 (for example: 3 x 2). the the(By the way, as noted in The Hitchhiker's Guide to Python, here is a common misconception aboutLambdaExpression of some things. OneLambdaAn expression creates a function that is not special, and uses an ordinarydefThe function created shows the same behavior. ) the - here are two ways to solve this problem. the the The most common solution is to create a closure that binds its parameters immediately by using the default parameters. For example: the 94 defmultipliers (): the return[LambdaX, I=I:I * x forIinchRange (4)] the Another option is that you can use the Functools.partial function: the 98 fromFunctoolsImportPartial About fromoperatorImportMul - defmultipliers ():101 return[Partial (Mul, i) forIinchRange (4)]102 question five: What will be the output of the following code? Say your answer and explain it? 103 defExtendlist (Val, list=[]): 104 List.append (val) the returnList106List1 = Extendlist (10)107List2 = Extendlist (123,[])108List3 = Extendlist ('a')109 the Print "List1 =%s"%List1111 Print "List2 =%s"%List2 the Print "list3 =%s"%List3113 How will you modify the definition of extendlist to produce the desired result the the the output of the above code is: the 117List1 = [10,'a']list2 = [123]LIST3 = [10,'a']118Many people mistakenly think that list1 should be equal to [10] and list3 should equal ['a']. It is assumed that the list's parameters will be set to its default value every time extendlist is called []. 119 - However, what actually happens is that the new default list is created only once when the function is defined. It then uses the same list when extendlist is not called by the specified list parameter. This is why when a function is defined, the expression is evaluated with the default argument, not when it is called. 121 122 therefore, List1 and list3 are the same list of operations. The "List2" is the independent list of operations it creates (by passing its own empty list as the value of the list ' parameter). 123 124 the definition of the Extendlist function can be modified as follows, but when no new list parameter is specified, a new listing is always started, which is more likely to be expected behavior. the 126 defExtendlist (Val, list=None):127 ifList isNone: -List = [] 129 List.append (val) the returnList131 with this improved implementation, the output will be: the 133List1 = [10]134List2 = [123]135LIST3 = ['a']
python-Five questions of the face