美文网首页
[code snippet]优先级的妙用

[code snippet]优先级的妙用

作者: 顾慎为 | 来源:发表于2018-05-24 23:24 被阅读17次

位置参数默认为None,但该参数默认是一个可迭代对象。如果不传参的话:

In [8]: def func(iter_obj=None):
   ...:     for item in iter_obj:
   ...:         print item
   ...:

In [9]: func()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-08a2da4138f6> in <module>()
----> 1 func()

<ipython-input-8-f880f4d9f102> in func(iter_obj)
      1 def func(iter_obj=None):
----> 2     for item in iter_obj:
      3         print item
      4

TypeError: 'NoneType' object is not iterable

通过优先级来对准备迭代的对象进行替换:

In [10]: def func(iter_obj=None):
    ...:     for item in iter_obj or ():
    ...:         print item
    ...:

In [11]: func()

来源:werkzeug.routing.Map__init__方法:

class Map(object):
    def __init__(self, rules=None, default_subdomain='', charset='utf-8',
                 strict_slashes=True, redirect_defaults=True,
                 converters=None, sort_parameters=False, sort_key=None,
                 encoding_errors='replace', host_matching=False):
       ...

        for rulefactory in rules or ():
            self.add(rulefactory)

相关文章

  • [code snippet]优先级的妙用

    位置参数默认为None,但该参数默认是一个可迭代对象。如果不传参的话: 通过优先级来对准备迭代的对象进行替换: 来...

  • 使用Code Snippet在Xcode中添加代码段

    使用Code Snippet在Xcode中添加代码段 自定义添加Code snippet步骤 1、打开Code S...

  • iOS开发奇技淫巧之Code Snippet

    什么是Code Snippet ? Code Snippet直译为代码片段,可能你对这个名词不太了解,但是我相信在...

  • 创建code snippet

    在xcode中再有的code snippet之外,可以创建自定义的code snippet,具体做法为选中sour...

  • Xcode 添加及删除code snippet Xcode如何拖

    1.什么是代码code snippet? 2.code snippet 存放的位置 3.如何自定义添加比如平常 我...

  • Code Snippet

    中文对齐 Unity3D 中 dotween 实现: 箭头向上跳动效果(见图1) 标准正态分布函数 sin函数的近...

  • Code Snippet

    参考文档Code Snippet 代码片断可以很好地提高编程效率 常用如initinlineBlocktypede...

  • Processing技巧:Snippets这件事

    Snippet是什么? Snippet或者Code Snippet是指一段可复用代码,通常,有经验的程序员或者一些...

  • Tag Validator

    题目Given a string representing a code snippet, you need to...

  • Python Snippet

    Useful Python Code Snippet Control and Loop: list compreh...

网友评论

      本文标题:[code snippet]优先级的妙用

      本文链接:https://www.haomeiwen.com/subject/pxbejftx.html