Table of Contents
回复内容:
Home Backend Development Python Tutorial Python 使用 list 作为函数参数时,参数是否会初始化?

Python 使用 list 作为函数参数时,参数是否会初始化?

Jun 06, 2016 pm 04:23 PM
append def print

看到了这样一段代码:

def foo(a, b=[]):
    b.append(a)
    print b
Copy after login

回复内容:

<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>
Copy after login
官方文档解释在这里:default args 的求值是在定义的时候,只做一次
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
[]
>>> 
Copy after login
啥都不用说,加一个id()输出b的所谓的地址,你就明白了

Python 使用 list 作为函数参数时,参数是否会初始化? no, def foo(a=[]) 这种函数参数写法叫 参数默认值,只会在函数声明是初始化一次。之后不会再变了。

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>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use java's StringBuilder.append() function to splice strings Use java's StringBuilder.append() function to splice strings Jul 26, 2023 am 09:18 AM

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

How to use append in python How to use append in python Nov 14, 2023 am 10:41 AM

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.

How to use append How to use append Oct 25, 2023 pm 03:14 PM

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 How to use the append() method of the StringBuilder class to splice strings in Java Jul 25, 2023 pm 03:05 PM

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

Introduction to Python functions: functions and usage examples of the print function Introduction to Python functions: functions and usage examples of the print function Nov 03, 2023 pm 04:33 PM

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

Where is print on the keyboard? Where is print on the keyboard? Jun 19, 2023 am 09:37 AM

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.

Using the print function in Python Using the print function in Python Feb 18, 2024 pm 02:48 PM

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,

What does print mean in vb What does print mean in vb Jan 18, 2021 am 10:47 AM

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.

See all articles