Skip to main content
 首页 » 编程设计

Python 新手 : What does this code do

2025年05月04日13hnrainll

这是来自 Google AppEngine 教程的片段。

application = webapp.WSGIApplication([('/', MainPage)], debug=True) 

我不太确定 debug=True 在构造函数调用中做了什么。 它是否创建一个名称为 debug 的局部变量,将 True 分配给它,并将其传递给构造函数,或者这是一种在构造函数中设置类实例成员变量值的方法?

请您参考如下方法:

Python 函数接受关键字参数。如果你这样定义一个函数:

def my_func(a, b='abc', c='def'): 
    print a, b, c 

你可以这样调用它:

my_func('hello', c='world') 

结果将是:

hello abc world 

您还可以使用特殊语法支持动态关键字参数:

def my_other_func(a, *b, **c): 
    print a, b, c 
  • *b 表示 b 变量将把 a 之后的所有非命名参数作为一个 元组 对象。
  • **c 表示 c 变量将采用所有命名参数,作为 dict 对象。

如果你这样调用函数:

my_other_func('hello', 'world', 'what a', state='fine', what='day') 

您将获得:

hello ('world', 'what a') {'state': 'fine', 'what': 'day'}