English 中文(简体)
基本原理: 如何检查一项功能回归的价值观?
原标题:Python basics: How to check if a function returns mutiple values?
  • 时间:2011-10-08 16:51:48
  •  标签:
  • python

A. 基本情况 但是,检查职能是否恢复某些价值的最佳方式是什么?

def hillupillu():
    a= None
    b="lillestalle"
    return a,b

if i and j in hillupillu(): #how can i check if i or j are empty? this is not doing it of course;-P
    print i,j 
最佳回答

如果你意味着你可以预测某些职能的回报价值的数量,那么,

i, j = hillupillu()

如果该功能确实有两个价值,则将产生<代码>ValueError。 您可以按照通常的<条码>、收集。 构造:

try:
    i, j = hillupillu()
except ValueError:
    print("Hey, I was expecting two values!")

This follows the common Python idiom of "asking for forgiveness, not permission". If hillupillu might raise a ValueError itself, you ll want to do an explicit check:

r = hillupillu()
if len(r) != 2:  # maybe check whether it s a tuple as well with isinstance(r, tuple)
    print("Hey, I was expecting two values!")
i, j = r

If you meant that you want to check for None in the returned values, then check for None in (i, j) in an if-clause.

问题回答

在从职能中获得价值后:

i, j = hillupillu()

您可核实价值是否为Noneis的操作者:

if i is None: ...

你们也可以只是检验真相价值的价值:

if i: ...
if(i == "" or j == ""):
   //execute code

象这样说,但如果你给它一个<代码>的话。 页: 1

if(i == None or j == None):
    //execute code

希望

您可以核实该职能的回报价值是否为:

r_value = foo(False)

x, y = None, None
if type(r_value) == tuple:
    x, y = r_value
else:
    x = r_value
    
print(x, y)

举例来说,如果已知该功能可以收回一个精确的长度2的图形(例如,采用<代码>return a, b),或一个单一的数值,则该功能就属于这种情况。 它可以扩大到处理其他职能可以退还其他期限的拖.的情况。

我不认为“Foo”的接受答案是正确的,原因有几个:

  • It will happily unpack other iterables that can be unpacked into two values, such as a list or string of lengths 2. This does not correspond to a return from a function that returned multiple values.
  • The thrown exception can be TypeError, not a ValueError.
  • The variables that store the return value are scoped to the try block, and so we can only deal with them inside that block.
  • It doesn t show how to handle the case where there is a single returned value.




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签