Q
函数中的可选标志
1
A
回答
7
比方说x
,y
和z
是需要argruments和opt
是可选的:
def f(x, y, z, opt=None):
# do required stuff
if opt is not None:
# do optional stuff
这可以用三个或四个参数来调用。你明白了。
0
def fextrao(x, y, a=3):
print "Got a", a
#do stuff
def fextrad(x, y, **args):
# args is a dict here
if args.has_key('a'):
print "got a"
# do stuff here
def fextrat(x, y, *args):
# args is a tuple here
if len(args) == 1:
print "Got tuple", args[0]
#do stuff
fextrao(1, 2)
fextrao(1, 2, 4)
fextrad(1, 2, a=3, b=4)
fextrat(1, 2, 3, 4)
0
你可以有默认值给你函数参数,类似于可选参数。事情是这样的:
def myFunction(required, optionalFlag=True):
if optionalFlag:
# do default
else:
# do something with optionalFlag
1
你可以使用还关键字参数:
def f(x, y, **kwargs):
if 'debug_output' in kwargs:
print 'Debug output'
然后你可以有你喜欢的尽可能多的..
f(1,2, debug_output=True, file_output=True)
相关问题
- 1. Perl6可选功能标志
- 2. PHP中函数的二进制标志
- 3. C++检查函数的“标志参数”
- 4. WinHttpSetOption函数的标志整数匹配
- 5. Python选项解析器:布尔标志与可选参数
- 6. SQL-Group By根据可选标志
- 7. linux recv()函数标志参数?
- 8. 模板函数中的可选参数
- 9. MVEL函数中的可选参数
- 10. c#函数中的可选参数
- 11. Python 3.5函数中的可选参数
- 12. 类函数中的可选参数
- 13. @depends标志打破我的PHPUnit函数中的参数
- 14. uint64标志或uint32标志[2]为32位编译器的函数参数?
- 15. 标志选定行
- 16. 可以调用DLL函数卸载DLL是否有dontcopy标志?
- 17. 可选的getopt函数
- 18. SSE内在函数检查零标志
- 19. 在vba函数中的可选范围
- 20. Swift - 使构造函数参数上的标签可选?
- 21. 如何读取GetHandleInformation函数中的标志
- 22. 夏普标志在这个Mathematica分割函数中的含义
- 23. 在使用标志执行的对象中停止函数
- 24. jQuery中的无效表达标志.ajax()回调函数
- 25. 如何在MVC 1.0路由中配置可选标志
- 26. 分析bash脚本中的参数/选项/标志
- 27. 处理可选的函数参数
- 28. Windsor可选的构造函数参数
- 29. VBA函数 - 参数是不可选的
- 30. 在标志中隐藏选项