重载*的东西 [英] overloading *something

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

问题描述

Hello All,


如何使用&方法调用中的任意类(例如,类myclass(object))表现得像

a list *什么"运营商?我的意思是:


myobj = myclass()


doit(* myobj)


我看过getitem,getslice和iter。如果不是其中之一,它是什么?


那么,**某事怎么样?运营商?


James


-

James Stroud

UCLA-DOE基因组学和蛋白质组学研究所

专栏951570

洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/

Hello All,

How does one make an arbitrary class (e.g. class myclass(object)) behave like
a list in method calls with the "*something" operator? What I mean is:

myobj = myclass()

doit(*myobj)

I''ve looked at getitem, getslice, and iter. What is it if not one of these?

And, how about the "**something" operator?

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/

推荐答案




James Stroud写道:


James Stroud wrote:
Hello All,

如何制作一个任意类(例如类myclass(对象) ))表示类似
方法调用中的列表,带有* something。运营商?我的意思是:


如果我理解你的问题,你需要将myclass基于列表。


class myclass(list):

def __init __(自我,*项目):

#如果需要更改项目

#如果需要,则启动其他属性

list .__ init __(self,* items)


然后下面的行应该可行。当然,它不会带来很多东西。

。 ;-)

myobj = myclass()

doit(* myobj)

我看过getitem,getslice和iter。如果不是其中之一,它是什么?

那么,**某事怎么样?运营商?

詹姆斯
Hello All,

How does one make an arbitrary class (e.g. class myclass(object)) behave like
a list in method calls with the "*something" operator? What I mean is:
You need to base myclass on a list if I understand your question.

class myclass(list):
def __init__(self, *items):
# change items if needed
# initate other attributes if needed
list.__init__(self, *items)

Then the line below should work. Of course it won''t do much with out
something in it. ;-)
myobj = myclass()

doit(*myobj)

I''ve looked at getitem, getslice, and iter. What is it if not one of these?

And, how about the "**something" operator?

James




一本字典几乎是相同的,除了从

字典的子类当然。


干杯,

Ron



A dictionary would be pretty much the same except subclassed from a
dictionary of course.

Cheers,
Ron


Ron Adam< rr * @ ronadam.com>写道:
Ron Adam <rr*@ronadam.com> wrote:
James Stroud写道:
James Stroud wrote:
Hello All,

如何制作一个任意的类(例如class myclass(object))表示类似于方法调用中的列表,带有* something。运营商?我的意思
是:
Hello All,

How does one make an arbitrary class (e.g. class myclass(object)) behave
like a list in method calls with the "*something" operator? What I mean
is:



如果我理解你的问题,你需要将myclass基于列表。



You need to base myclass on a list if I understand your question.




没必要,你需要的只是__iter__:



Not necessary, all you need is __iter__:

def f(* a):打印
.... class X(object):
.... def __iter __(self):return iter(xrange(4))

.... f(* X( ))
(0,1,2,3)
def f(*a): print a .... class X(object): .... def __iter__(self): return iter(xrange(4))
.... f(*X()) (0, 1, 2, 3)


我看过getitem,getslice和iter。如果不是其中之一,它是什么?


显然詹姆斯没有以正确的方式看__iter__!


而且,**某事怎么样?运营商?

詹姆斯

I''ve looked at getitem, getslice, and iter. What is it if not one of these?
Obviously James hadn''t looked at __iter__ in the RIGHT way!

And, how about the "**something" operator?

James



一个字典几乎是相同的,除了从字典中划分的字典。



A dictionary would be pretty much the same except subclassed from a
dictionary of course.




我相信这个是正确的(但我还没有深入检查过!)。

Alex



I believe this one is correct (but I have not checked in-depth!).
Alex


James Stroud写道:
James Stroud wrote:
Hello All,

如何使一个任意类(例如类myclass(object))表现得像
方法调用中的列表用*某事运营商?我的意思是:

myobj = myclass()

doit(* myobj)


让它可迭代:
Hello All,

How does one make an arbitrary class (e.g. class myclass(object)) behave like
a list in method calls with the "*something" operator? What I mean is:

myobj = myclass()

doit(*myobj)
Make it iterable:
class Foo(object):
... def __iter __(self):

...收益率1

...收益率2

...收益率3

... def bar(* args) :
... print args

... bar(* Foo())
class Foo(object): ... def __iter__(self):
... yield 1
... yield 2
... yield 3
... def bar(*args): ... print args
... bar(*Foo())



(1 ,2,3)


而且,**某事怎么样?运营商?


(1, 2, 3)

And, how about the "**something" operator?




使用字典。



Use a dictionary.


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

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