无法重现functools库的示例

时间:2019-06-14 15:51:07

标签: python python-3.x jupyter-notebook functools single-dispatch

我正在研究python中的#protect_from_forgery with: :exception, prepend: true #before_action :authenticate_user! 库。但是,当我从装饰器文档中复制示例时

functools

我得到的结果与文档中报告的结果不同。

@singledispatch

应评估为:

from functools import singledispatch

@singledispatch
def fun(arg, verbose=False):
    if verbose:
        print("Let me just say,", end=" ")
    print(arg)

@fun.register
def _(arg: int, verbose=False):
    if verbose:
        print("Strength in numbers, eh?", end=" ")
    print(arg)

@fun.register
def _(arg: list, verbose=False):
    if verbose:
        print("Enumerate this:")
    for i, elem in enumerate(arg):
        print(i, elem)

但是在我的juypter笔记本中却没有。

>>> fun("Hello, world.")
Hello, world.
>>> fun("test.", verbose=True)
Let me just say, test.
>>> fun(42, verbose=True)
Strength in numbers, eh? 42
>>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
Enumerate this:
0 spam
1 spam
2 eggs
3 spam

0 个答案:

没有答案