What will the output of the following code be? Say your answer and explain?
defExtendlist (Val, list=[]): List.append (val)returnListlist1= Extendlist (10) List2= Extendlist (123, []) List3= Extendlist ('a')Print "List1 =%s"%List1Print "List2 =%s"%List2Print "list3 =%s"% LIST3
How you will modify extendList
the definition to produce the desired result
The output of the above code is:
[Ten, ' a '][123][Ten, ' a ']
Many people mistakenly think that list1
should be equal to [10]
and list3
should be equal [‘a‘]
. The argument is assumed to be list
extendList
set to its default value each time it is called []
.
However, what actually happens is that the new default list is created only once when the function is defined. It then extendList
uses the same list when it 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.
So, list1
and list3
is the same list of operations. 是操作的它创建的独立的列表(通过传递它自己的空列表作为
the value of the "List2 list" parameter).
extendList
the definition of a function can be modified as follows , but when no new list
parameters are specified, a new list is always started, which is more likely to be expected behavior.
def Extendlist (Val, list=None): if is none: = [] List.append (val) return list
With this improved implementation, the output will be:
[123] [' a ']
Python objects occupy memory size
Http://www.th7.cn/Program/Python/2012/03/02/61741.shtml
Tell me about Dict. The items () method differs from the Iteritems () method
List of D's (key, value) pairs, as 2-tuples-a iterator over the (key, value) items of D
Python Pass Usage
1. Empty statement does nothing
2. Guaranteed full format 3, guaranteed semantic integrity Python sethttp://blog.csdn.net/business122/article/details/7541486
Description Os,sys module is different, and enumerate common module method?
Official explanation:
Os:this module provides a portable the by using operating system dependent functionality.
Translation: Provides a convenient way to use operating system functions.
Sys:this module provides access to some variables used or maintained by the interpreter and to functions that interact Str Ongly with the interpreter.
Translation: Provides access to variables used or maintained by the interpreter and functions used in interaction with the interpreter.
Python memory management
Http://developer.51cto.com/art/201007/213585.htm
Python-13-2