【问题标题】:Variable assignment inside a function argument函数参数内的变量赋值
【发布时间】:2016-07-18 03:27:45
【问题描述】:
def auto_detect_serial_unix(preferred_list=['*']):

当这个函数被调用时,参数会发生什么?

【问题讨论】:

标签: python function


【解决方案1】:

如果没有向auto_detect_serial_unix 传递任何内容,则将preferred_list 设置为['*']。否则,你传入的设置为preferred_list

>>> def auto_detect_serial_unix(preferred_list=['*']):
...     print preferred_list
... 
>>> auto_detect_serial_unix()
['*']
>>> auto_detect_serial_unix(['new', 'list'])
['new', 'list']
>>> 

【讨论】:

    【解决方案2】:

    如果使用参数调用 auto_detect_serial_unix,则 preferred_list 将具有该参数的值。

    否则,如果在没有参数的情况下调用 auto_detect_serial_unix,则 preferred_list 将具有给定的默认值。

    【讨论】:

      猜你喜欢
      • 2018-07-08
      • 1970-01-01
      • 2018-03-12
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多