确定输出参数的数量 [英] determining the number of output arguments

查看:63
本文介绍了确定输出参数的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


def测试(数据):


i =?这是我遇到麻烦的行


如果我= = 1:返回数据

else:返回数据[:i]


a,b,c,d = test([1,2,3,4])


如何根据定义的输出参数数设置i在

(a,b,c,d)?


谢谢,

Darren

解决方案

2004年11月14日星期日17:12:24 -0500,Darren Dale< dd ** @ cornell.edu>

声明了在comp.lang.python中跟随:

你好,

def测试(数据):

i =?这是我遇到麻烦的行

如果我= = 1:返回数据
否则:返回数据[:i]

a,b,c, d = test([1,2,3,4])

如何根据
(a,b,c,d)中定义的输出参数的数量来设置i?


这是相当令人困惑的...


你期望收到什么


a ,b,c,d =测试([1,2,3,4,5])


注意,你只是将一个参数传递给函数,并且

显然想要一个元组作为回报...


问题:如果长度不匹配(

目的地中的4项,以及5 in in列表)你得到一个例外。否则你

就可以使用


a,b,c,d =元组([1,2,3,4])

如果您实际上尝试使用未知数量的

输入参数(而不是列表的单个参数)...

< blockquote class =post_quotes>

def test(* data):
.... print len(data)

.... test([ 1,2,3,4])
1测试(1,2,3,4)
4
def测试(*数据):
....返回数据

....测试([1,2,3,4])
([1,2,3,4],)测试(1,2,3,4)
(1,2,3,4)def测试(*数据):
....返回列表(数据)

.... test([1, 2,3,4])
[[1,2,3,4]]测试(1,2,3,4)
[1,2,3,4]



谢谢,
Darren


- ============= ===================================== ============< ;
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG<
wu******@dm.net | Bestiaria支持人员<
========================================= ========= ============<
主页:< http://www.dm.net/~wulfraed/> <
溢出页面:< http://wlfraed.home.netcom.com/> <



Darren Dale写道:

你好,

def测试(数据):

我=?这是我遇到麻烦的行

如果我= = 1:返回数据
否则:返回数据[:i]

a,b,c, d = test([1,2,3,4])

如何根据
(a,b,c,d)中定义的输出参数的数量来设置i?




这样的东西:


def test(* args,** kwargs):

i = len(args)+ len(kwargs)


应该有效。但请注意,您提供的用法示例将导致i

的值为1 - 您传递的是一个参数(这是一个

列表) 。


当然,如果你总是将一个序列传递到你的

函数中,你想得到该序列的长度,那么它的价格非常简单:


def测试(数据):

i = len(数据)

返回数据[:i]


但请注意,此功能实际上是无操作的。

大概你打算做一些改变数据的事情,这可能会改变它的长度吗?否则,在for循环/列表comp中就地修改

列表(或其副本)会更简单,而不必担心

关于总长度。


Jeff Shannon

技术员/程序员

Credit International

Fernando Perez< fp ******* @ yahoo.com>写道:

怀疑通过使用sys._getframe(),dis
和inspect模块玩非常讨厌的技巧,你可能_could_得到这个信息,在
至少如果调用者不是C扩展模块。但我甚至不是100%确定这是有效的,而且肯定会是那种黑魔法我确定你不会问。但鉴于这里的专业水平,我更好地掩盖了我的屁股; - )




是的,这是Jp提到的食谱配方 - 确切地说,萨米Hangaslammi的

食谱284742。是的,它会进入印刷的第二版

版本(我现在应该正在处理 - 截止日期

关闭,帮助,帮助! - )。

Alex


Hello,

def test(data):

i = ? This is the line I have trouble with

if i==1: return data
else: return data[:i]

a,b,c,d = test([1,2,3,4])

How can I set i based on the number of output arguments defined in
(a,b,c,d)?

Thank you,
Darren

解决方案

On Sun, 14 Nov 2004 17:12:24 -0500, Darren Dale <dd**@cornell.edu>
declaimed the following in comp.lang.python:

Hello,

def test(data):

i = ? This is the line I have trouble with

if i==1: return data
else: return data[:i]

a,b,c,d = test([1,2,3,4])

How can I set i based on the number of output arguments defined in
(a,b,c,d)?

This is rather confusing...

What do you expect to receive for

a,b,c,d = test([1,2,3,4,5])

Note, you are only passing ONE argument into the function, and
apparently wanting a tuple in return...

Problem: in the case of mismatched lengths (4 items in
destination, and 5 in the list) you get an exception. Otherwise you
could just use

a,b,c,d = tuple([1,2,3,4])
If you are actually trying to work with an unknown number of
input arguments (rather than a single argument of a list)...

def test(*data): .... print len(data)
.... test([1,2,3,4]) 1 test(1,2,3,4) 4
def test(*data): .... return data
.... test([1,2,3,4]) ([1, 2, 3, 4],) test(1,2,3,4) (1, 2, 3, 4) def test(*data): .... return list(data)
.... test([1,2,3,4]) [[1, 2, 3, 4]] test(1,2,3,4) [1, 2, 3, 4]


Thank you,
Darren
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <



Darren Dale wrote:

Hello,

def test(data):

i = ? This is the line I have trouble with

if i==1: return data
else: return data[:i]

a,b,c,d = test([1,2,3,4])

How can I set i based on the number of output arguments defined in
(a,b,c,d)?



Something like this:

def test(*args, **kwargs):
i = len(args) + len(kwargs)

should work. But note that the usage example you gave will result in i
having a value of 1 -- you''re passing in a single argument (which is a
list).

Of course, if you''re always going to be passing a sequence into your
function, and you want to get the length of that sequence, then it''s
pretty simple:

def test(data):
i = len(data)
return data[:i]

Note, however, that this function is effectively a no-op as it stands.
Presumably you''re intending to do something to transform data, which may
change its length? Otherwise, it would be simpler to just modify the
list (or a copy of it) in-place in a for loop / list comp, and not worry
about the length at all.

Jeff Shannon
Technician/Programmer
Credit International


Fernando Perez <fp*******@yahoo.com> wrote:

suspect that by playing very nasty tricks with sys._getframe(), the dis
and the inspect modules, you probably _could_ get to this information, at
least if the caller is NOT a C extension module. But I''m not even 100%
sure this works, and it would most certainly the kind of black magic I''m
sure you are not asking about. But given the level of expertise here, I
better cover my ass ;-)



Yep, that''s the cookbook recipe Jp was mentioning -- Sami Hangaslammi''s
recipe 284742, to be precise. Yep, it _is_ going into the printed 2nd
edition (which I''m supposed to be working on right now -- deadline
closing in, help, help!-).
Alex


这篇关于确定输出参数的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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