有没有办法在 python 中返回任何内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15650614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:37:54  来源:igfitidea点击:

Is there a way to return literally nothing in python?

python

提问by Kirca

I'm learning python and I was just wondering if I there was a way to write a code that does something like:

我正在学习 python,我只是想知道是否有一种方法可以编写类似以下内容的代码:

def f(x):
    if x>1:
       return(x)
    else:
        # don't return anything

I'm asking about the else part of the code. I need to not return anything if x<=1, returning Noneisn't acceptable.

我问的是代码的其他部分。如果x<=1,返回None是不可接受的,我不需要返回任何东西。

采纳答案by Blckknght

There is no such thing as "returning nothing" in Python. Every function returns some value (unless it raises an exception). If no explicit returnstatement is used, Python treats it as returning None.

Python 中没有“什么都不返回”这样的东西。每个函数都会返回一些值(除非它引发异常)。如果未使用显式return语句,Python 会将其视为返回None.

So, you need to think about what is most appropriate for your function. Either you should return None(or some other sentinel value) and add appropriate logic to your calling code to detect this, or you should raise an exception (which the calling code can catch, if it wants to).

因此,您需要考虑什么最适合您的功能。要么你应该返回None(或一些其他的哨兵值)并向你的调用代码添加适当的逻辑来检测这个,或者你应该引发一个异常(调用代码可以捕获,如果它想要的话)。

回答by Sinkingpoint

To literally return 'nothing' use pass, which basically returns the value None if put in a function(Functions must return a value, so why not 'nothing'). You can do this explicitly and return Noneyourself though.

要从字面上返回 'nothing' 使用pass,它基本上返回值 None 如果放在函数中(函数必须返回一个值,那么为什么不是 'nothing')。不过,您可以明确地和您return None自己这样做。

So either:

所以要么:

if x>1:
    return(x)
else:
    pass

or

或者

if x>1:
    return(x)
else:
    return None

will do the trick.

会做的伎俩。

回答by dawg

You can do something like this:

你可以这样做:

>>> def f(x):
...    return x if x>1 else None
... 
>>> f(1),f(2)
(None, 2)

It will appear to 'return nothing':

它似乎“不返回任何内容”:

>>> f(1)
>>> 

But even the alternative returns None:

但即使是替代方案也没有返回:

>>> def f2(x): 
...    if x>1: return x
... 
>>> f2(1),f2(2)
(None, 2)

Or:

或者:

>>> def f2(x):
...    if x>1: 
...        return x
...    else:
...        pass
... 
>>> f2(1),f2(2)
(None, 2)

So they are functionally the same no matter how you write it.

因此,无论您如何编写,它们在功能上都是相同的。

回答by Josef

How about this?

这个怎么样?

def function(x):
     try:
         x = x+1
         return (x)
     except:
         return ('')

回答by Swetha Varikuti

You can use lambda function with filter to return a list of values that pass if condition.

您可以使用带过滤器的 lambda 函数返回通过 if 条件的值列表。

Example:

例子:

    myList = [1,20,5,50,6,7,2,100]
    result = list(filter(lambda a: a if a<10 else None, (item for item in myList)))
    print(result)

Output:

输出:

    [1,5,6,7,2]

回答by Hrishikesh Shukla

There's nothing like returning nothing but what you are trying to do can be done by using an empty returnstatement. It returns a None.

没有什么比什么都不返回,但是您可以通过使用空return语句来完成您尝试做的事情。它返回一个None.

You can see an example below:

您可以在下面看到一个示例:

if 'account' in command:
    account()

def account():
    talkToMe('We need to verify your identity for this. Please cooperate.')
    talkToMe('May I know your account number please?')
    acc_number = myCommand()
    talkToMe('you said your account number is '+acc_number+'. Is it right?')
    confirmation = myCommand()
    if confirmation!='yes' or 'correct' or 'yeah':
        talkToMe('Let\'s try again!')
        account()
    else:
        talkToMe('please wait!')
    return

This will return nothing to calling function but will stop the execution and reach to the calling function.

这不会向调用函数返回任何内容,但会停止执行并到达调用函数。

回答by Sumit Singh

As mentioned above by others, it will always return None in your case. However you can replace it with white space if you don't want to return None type

正如其他人上面提到的,在你的情况下它总是会返回 None 。但是,如果您不想返回 None 类型,则可以将其替换为空格

def test(x):
    if x >1:
        return(x)
    else:
        return print('')