简体   繁体   English

Python中的不同返回类型

[英]Different Return Types in Python

I have a function that returns two different types ( bool and int ): 我有一个函数返回两个不同的类型( boolint ):

def limit_reached():
    rate_limit = api.GetRateLimitStatus()
    if not rate_limit['remaining_hits'] == 0:
        return False
    else:
        return rate_limit['remaining_hits']

Basically, I want to be able to call the function to see if the "limit" limit has been reached. 基本上,我希望能够调用该函数以查看是否已达到“限制”限制。 If it has not, before continuing, print how many 'remaining_hits' are left. 如果没有,请在继续之前打印出'remaining_hits'

So far I have this, but can't figure out how to efficiently show 'remaining_hits' without calling the GetRateLimitStatus() function again: 到目前为止,我已经有了这个,但是在不再次调用GetRateLimitStatus()函数'remaining_hits'情况下,无法弄清楚如何有效显示'remaining_hits'

if limit_reached():
    print "Limit reached. Exiting"
    exit()
else:
    ##Print remaining hits
    ##Continue...

Also, if it helps, I am using the Twitter API for Python 另外,如果有帮助,我正在使用PythonTwitter API

In python, the integer '0' is identically equivalent to 'False'. 在python中,整数'0'等同于'False'。 So, for any and every truth test in Python, the integer '0' evaluates to false. 因此,对于Python中的所有真相测试,整数'0'的计算结果均为false。

For what I see, you can adapt your code to use this fact. 对于我所看到的,您可以修改您的代码以使用此事实。

Hope it helps. 希望能帮助到你。

Store the result in a variable? 将结果存储在变量中?

remaining = limit_reached()
if not remaining:
    print "Limit reached."
else:
    print "Remaining:", remaining

PS you can also return 0 when the limit was reached... No need to return False when it actually means 0 . PS,当达到限制时,您还可以返回0 ……实际上意味着0时,无需返回False

You should redefine your limit_reached function: 您应该重新定义limit_reached函数:

def limit_reached():
    return api.GetRateLimitStatus()['remaining_hits']

then something like: 然后是这样的:

remaining = limit_reached()
if remaining: # or 'remaining > 0' if you want to be explicit
    ##Print remaining hits
    ##Continue...
else:
    print "Limit reached. Exiting"
    exit()

As a commenter pointed out, returning different variable types is bad style. 正如评论员指出的那样,返回不同的变量类型是不好的样式。 It is fairly easy to always return a boolean value, like so: 总是很容易返回布尔值,就像这样:

def limit_reached():
    rate_limit = api.GetRateLimitStatus()
    return rate_limit['remaining_hits'] == 0

rate_limit['remaining_hits'] == 0 is a complete statement that will return a 'true' or 'false' value, the result of which you can return from your function. rate_limit['remaining_hits'] == 0是一条完整的语句,它将返回“ true”或“ false”值,您可以从函数中返回结果。 Note that you do need to use two equals signs. 请注意,您确实需要使用两个等号。

If you need the integer value itself, you can always return that instead, and test the condition in your other code, like so: 如果您本身需要整数值,则始终可以返回该值,并在其他代码中测试条件,如下所示:

def limit_reached():
    rate_limit = api.GetRateLimitStatus()
    return rate_limit['remaining_hits']

if limit_reached() == 0:
    print "Limit reached. Exiting"
    exit()
else:
    ##Print remaining hits
    ##Continue...

Or, you could take advantage of the fact that all numbers (integer, float, double, it doesn't matter) with an exact value of 0 are treated as false (other constants treated as false include [] , () , '' , or '{}' - see here ). 或者,您可以利用以下事实:所有精确值为0数字(整数,浮点数,双精度数都没有关系)被视为false (其他常量被视为false常量包括[]()''或'{}'-参见此处 )。

if limit_reached():
    ##Print remaining hits
    ##Continue...
else:
    print "Limit reached. Exiting"
    exit()

Note that the clauses have been reversed in this case. 请注意,在这种情况下,这些子句已被颠倒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM