转发* arg参数 [英] forwarding *arg parameter

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

问题描述

>> def g(* arg):

.... return arg

....


>> g(''foo'',''bar'')



(''foo'',''bar'')


>>#似乎合理



....


>> g(g(''foo'',''bar''))



((''foo'',''bar''),)

< blockquote class =post_quotes>


>>#不太好,什么g应该返回以摆脱外部元组



TV

解决方案

Tuomas schrieb:


>> def g(* arg):



... return arg

...


>> g('' foo'',''bar'')



(''foo'',''bar'')


>>#似乎合理



...


>> g(g(''foo'',''bar''))



((''foo'',''bar''),)


>>#不是这样好的,什么g应该返回去除外部元组



g(* g(''foo'',''bar ''))

*和**是对称的 - 它们捕获省略号参数,并且它们是
使得iterables / dicts作为位置/命名参数传递。


Diez


Diez B. Roggisch写道:


Tuomas schrieb:< br>


> >> def g(* arg):


... return arg
...


> >> g(''foo'',''bar'')


(''foo'',''bar'')


> >>#似乎合理


...


> >> g(g(''foo'',''bar''))


((''foo'',''bar''),)


> >>#不太好,什么g应该返回以摆脱外部元组




g(* g(''' foo'',''bar''))


*和**是对称的 - 它们捕获省略号参数,并且它们

make iterables / dicts作为位置/命名参数传递。


Diez



谢谢Diez


如果我想要结果(''foo'',''bar'')这个案例怎么样呢


>> def f(* arg):



.... return g(arg)

....


>> f(' 'foo'',''bar'')



((''foo'',''bar''), )


>> def h(* arg):



.... return arg [0]

....

< blockquote class =post_quotes>
>> g = h
f(''foo'',''bar'')



(''foo'',''bar'')


哪里可以知道在转发arg时应该使用arg [0] ?


电视


Tuomas schrieb:


>> def g(* arg):



... return arg

...


>> g(''foo'',''bar '')



(''foo'',''bar'')


>>#似乎合理



...


>> g (g(''foo'',''bar''))



((''foo'',''bar'') ,)


>>#不太好,应该返回什么以摆脱外部元组



TV



使用以下:


>> g(* g(''foo'',''bar''))



(''foo'',''bar'')


否则,你必须检查是否arg是一个1元组的公司坚持使用

元组和剥离元组那么。


例如


>> def g(* arg):



....如果isinstance(arg [0],元组)返回arg [0],否则arg

....


>> g(''foo'',''bar'')



(''foo'',''bar'')

< blockquote class =post_quotes>


>> g(g(''foo'',''bar''))



(''foo'',''bar'')


>>def g(*arg):
.... return arg
....

>>g(''foo'', ''bar'')

(''foo'', ''bar'')

>># seems reasonable

....

>>g(g(''foo'', ''bar''))

((''foo'', ''bar''),)

>># not so good, what g should return to get rid of the outer tuple

TV

解决方案

Tuomas schrieb:

>>def g(*arg):

... return arg
...

>>g(''foo'', ''bar'')

(''foo'', ''bar'')

>># seems reasonable

...

>>g(g(''foo'', ''bar''))

((''foo'', ''bar''),)

>># not so good, what g should return to get rid of the outer tuple

g(*g(''foo'', ''bar''))
* and ** are the symetric - they capture ellipsis arguments, and they
make iterables/dicts passed as positional/named arguments.

Diez


Diez B. Roggisch wrote:

Tuomas schrieb:

> >>def g(*arg):

... return arg
...

> >>g(''foo'', ''bar'')

(''foo'', ''bar'')

> >># seems reasonable

...

> >>g(g(''foo'', ''bar''))

((''foo'', ''bar''),)

> >># not so good, what g should return to get rid of the outer tuple



g(*g(''foo'', ''bar''))
* and ** are the symetric - they capture ellipsis arguments, and they
make iterables/dicts passed as positional/named arguments.

Diez

Thanks Diez

And what about this case if I want the result (''foo'', ''bar'')

>>def f(*arg):

.... return g(arg)
....

>>f(''foo'', ''bar'')

((''foo'', ''bar''),)

>>def h(*arg):

.... return arg[0]
....

>>g=h
f(''foo'', ''bar'')

(''foo'', ''bar'')

Where can g know it should use arg[0] when arg is forwarded?

TV


Tuomas schrieb:

>>def g(*arg):

... return arg
...

>>g(''foo'', ''bar'')

(''foo'', ''bar'')

>># seems reasonable

...

>>g(g(''foo'', ''bar''))

((''foo'', ''bar''),)

>># not so good, what g should return to get rid of the outer tuple


TV

Use the following then:

>>g(*g(''foo'', ''bar''))

(''foo'', ''bar'')

Otherwise, you would have to check if arg is a 1-tuple consisting of a
tuple and "strip" it out then.

e.g.

>>def g(*arg):

.... return arg[0] if isinstance(arg[0], tuple) else arg
....

>>g(''foo'', ''bar'')

(''foo'', ''bar'')

>>g(g(''foo'', ''bar''))

(''foo'', ''bar'')


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

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