Python 使用 list 作为函数参数时,参数是否会初始化?
看到了这样一段代码:
def foo(a, b=[]): b.append(a) print b
回复内容:
<span class="o">>>></span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">bar</span><span class="o">=</span><span class="p">[]):</span> <span class="o">...</span> <span class="k">return</span> <span class="n">bar</span> <span class="o">>>></span> <span class="n">foo</span><span class="o">.</span><span class="n">func_name</span> <span class="s">'foo'</span> <span class="o">>>></span> <span class="n">foo</span><span class="o">.</span><span class="n">func_defaults</span> <span class="p">([],)</span> <span class="o">>>></span> <span class="n">foo</span><span class="p">()</span> <span class="ow">is</span> <span class="n">foo</span><span class="o">.</span><span class="n">func_defaults</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="bp">True</span>
4. More Control Flow Tools
但是……
>>> def f(a, b=[]): ... b.append(a) ... print b ... >>> f(1) [1] >>> f(1) [1, 1] >>> def f(a, b=None): ... b = b if b is not None else [] ... b.append(a) ... print b ... >>> f(1) [1] >>> f(1) [1] >>> f(1) [1] >>> a = [] >>> b = [] >>> a.append(1) >>> b [] >>>

note, 建议了解一下 def foo(a=[])和 foo(a=[])的区别:前者是参数默认值,后者是keyword arguments. 还有这种def foo(*args, **kargs) 和 这种 foo(*args, **kargs), 都是有细微区别的。 不会的, 默认值之间是共享的, 只会创建一次, 并不会每次创建一个新的对象. 也就是说使用可变对象作为函数的默认值时会导致函数的混乱. 同理使用字典作为默认参数,会得出类似的返回.
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">,</span> <span class="n">fdict</span><span class="o">=</span><span class="p">{}):</span> <span class="n">fdict</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span> <span class="k">print</span> <span class="n">fdict</span> <span class="n">foo</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="n">foo</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Use Java's StringBuilder.append() function to splice strings. In Java programming, string splicing is a very common operation. In order to efficiently splice strings, Java provides the StringBuilder class, whose append() function can quickly join multiple strings together. StringBuilder is a variable character sequence that is more efficient to use than the String class. When we need to concatenate a large number of strings, use

In Python, append() is a method of the list object that is used to add an element to the end of the list. It should be noted that the append() method can only be used for list objects and cannot be used for other types of objects. In addition, the append() method will directly modify the original list without returning a new list.

append is a commonly used command line tool used to append the contents of one file to the end of another file. The usage of the append command is "append [option] source file target file", where the source file is the file to be appended and the target file is the file to be appended.

How to use the append() method of the StringBuilder class to splice strings in Java. Splicing strings is a common operation in Java. During the splicing process, if the basic String class is used for string addition, new String objects will be created frequently, thus affecting performance. In order to solve this problem, Java provides the StringBuilder class to perform string splicing operations. StringBuilder class is one of the Java

Python is a popular programming language designed to make computer programming simpler and easier to understand. In Python, using the print function to output text to the console is a basic task. In this article, we'll introduce Python's print function, explore its capabilities and usage examples, and provide code examples to help you better understand how to use the function. Python's print function is a built-in function used to output text and the value of a variable. Its syntax is very simple. You just need to

The printscreen key is on the arrow keys of the keyboard device, with the words "prtsc sysrq" on it, and is located to the right of f12. If there is no button with the word "prtsc sysrq", you can find "fn" and "insert(prt sc)", click "fn" first, and then click "insert(PRT sc)" to realize the printscreen screenshot function.

Python is a simple and easy-to-learn high-level programming language that is widely used in data analysis, artificial intelligence, web development and other fields. In Python, print is a commonly used function used to output results or debugging information on the screen. This article will introduce the usage of the print function in detail and provide specific code examples to help readers better master it. First, the print function can accept multiple parameters and print them to the screen. These parameters can be strings, integers, floating point numbers, etc., or even variables,

Print in VB is an output statement. Under the WINDOWS graphical interface, this statement is not necessary; and in the VB.NET version, Print as a print output no longer exists.
