仅当值不为 None 时才应用函数的 Python 习语 [英] Python idiom for applying a function only when value is not None

查看:44
本文介绍了仅当值不为 None 时才应用函数的 Python 习语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个函数正在接收许多都是字符串但需要以各种方式解析的值,例如

vote_count = int(input_1)分数 = 浮点数(输入_2)人 = 人(输入_3)

这一切都很好,除了输入也可以是 None 并且在这种情况下,我不想解析值,而是将 None 分配到左侧手边.这可以通过

完成

vote_count = int(input_1) 如果 input_1 不是 None else None...

但这似乎不太可读,尤其是像这样的许多重复行.我正在考虑定义一个简化这个的函数,比如

def whendefined(func, value):如果值不是,则返回 func(value) None else None

可以像这样使用

vote_count = whendefined(int, input_1)...

我的问题是,这有什么共同的习语吗?可能使用内置的 Python 函数?即使没有,这样的函数有没有常用的名称?

解决方案

在其他语言中,有 选项输入,有点不同(用类型系统解决问题),但有相同的动机(如何处理空值).

在 Python 中,更多地关注此类事情的运行时检测,因此您可以使用无检测保护(而不是 Option 类型所做的数据)包装函数.

你可以写一个装饰器,它只在参数不是 None 时才执行函数:

def 选项(功能):def 包装器(*args, **kwargs):如果 len(args) >0 和 args[0] 不是 None:返回函数(*args, **kwargs)返回包装器

您可能应该调整第三行,使其更适合您正在处理的数据类型.

正在使用中:

@option定义选项打印(输入):返回输入 + "!!">>>选项打印(无)# 没有什么>>>选项打印(你好")'你好!!'

并带有返回值

@optiondef optioninc(输入):返回输入 + 1>>>optioninc(无)>>># 没有什么>>>期权公司(100)101

或者包装一个类型构造函数

<预><代码>>>>int_or_none = 选项(整数)>>>int_or_none(无)# 没有什么>>>int_or_none(12)12

A function is receiving a number of values that are all strings but need to be parsed in various ways, e.g.

vote_count = int(input_1)
score = float(input_2)
person = Person(input_3)

This is all fine except the inputs can also be None and in this case, instead of parsing the values I would like to end up with None assigned to the left hand side. This can be done with

vote_count = int(input_1) if input_1 is not None else None
...

but this seems much less readable especially with many repeated lines like this one. I'm considering defining a function that simplifies this, something like

def whendefined(func, value):
    return func(value) if value is not None else None

which could be used like

vote_count = whendefined(int, input_1)
...

My question is, is there a common idiom for this? Possibly using built-in Python functions? Even if not, is there a commonly used name for a function like this?

解决方案

In other languages there's Option typing, which is a bit different (solves the problem with a type system), but has the same motivation (what do do about nulls).

In Python there's more of a focus on runtime detection of this kind of thing, so you can wrap the function with an None-detecting guard (rather the data which is what Option typing does).

You could write a decorator that only executes a function if the argument is not None:

def option(function):
    def wrapper(*args, **kwargs):
        if len(args) > 0 and args[0] is not None:
          return function(*args, **kwargs)
    return wrapper

You should probably adapt that third line to be more suitable to the kind of data you're working with.

In use:

@option
def optionprint(inp):
    return inp + "!!"

>>> optionprint(None)
# Nothing

>>> optionprint("hello")
'hello!!'

and with a return value

@option
def optioninc(input):
    return input + 1

>>> optioninc(None)
>>> # Nothing

>>> optioninc(100)
101

or wrap a type-constructing function

>>> int_or_none = option(int)
>>> int_or_none(None)
# Nothing
>>> int_or_none(12)
12

这篇关于仅当值不为 None 时才应用函数的 Python 习语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆