Python-13-2

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.