输出类定义中的所有成员作为列表

发布于 2024-10-08 11:05:22 字数 569 浏览 3 评论 0原文

你好 我有一个类,本质上是一个包含一堆对象的列表对象。我尝试定义一个方法来返回所有成员,但失败了。 我的代码是:

class Stadion:
    def __init__(self,):
        self.__Tribuner=[]

    def setTribuner(self,tribuner):
        self.__Tribuner.append(tribuner)
    def getTribuner(self):
        pass
    def getAntallTribuner(self):
        antall=len(self.__Tribuner)
        return antall
    def addTribune(self,tribune):
        self.__Tribuner.append(tribune)
    def getTribuner(self):
        for tr in self.__Tribuner:
            return tr

我的问题是 getTribuner 方法。

/安迪.l

Hi
I've got a class that essentially is a list object holding a bunch of objects. I try to define a method to return all members, but it fails.
My code is:

class Stadion:
    def __init__(self,):
        self.__Tribuner=[]

    def setTribuner(self,tribuner):
        self.__Tribuner.append(tribuner)
    def getTribuner(self):
        pass
    def getAntallTribuner(self):
        antall=len(self.__Tribuner)
        return antall
    def addTribune(self,tribune):
        self.__Tribuner.append(tribune)
    def getTribuner(self):
        for tr in self.__Tribuner:
            return tr

Mye problem is the getTribuner method.

/Andy.l

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨离汐 2024-10-15 11:05:22

也许你想要

def getTribuner(self):
    return self.__Tribuner

或者

def getTribuner(self):
    for tr in self.__Tribuner:
        yield tr

我建议公开标准 Python 序列接口 而不是您的自定义方法名称。在您的情况下,从 list 派生 Stadion 会很容易。

Maybe you want

def getTribuner(self):
    return self.__Tribuner

or

def getTribuner(self):
    for tr in self.__Tribuner:
        yield tr

I would recommend to expose the standard Python sequence interface instead of your custom method names. In your case it would be easiset to just derive Stadion from list.

客…行舟 2024-10-15 11:05:22

第一次点击return tr时,它将退出。您要么想要返回一个生成器 (yield tr),要么只是返回 self.__Tribuner

The first time it hits return tr, it will quit. You either want to return a generator (yield tr) or just return self.__Tribuner.

晨光如昨 2024-10-15 11:05:22

如果您想迭代这些项目,请将 getTribuner 中的 return 替换为 yield

If you're wanting to iterate through the items, swap return for yield in getTribuner.

小帐篷 2024-10-15 11:05:22

尝试:

def getTribuner(self):
    return self.__Tribuner

如果不想立即返回列表,您可以使用 生成器,如下所示:

def getTribuner(self):
    for tr in self.__Tribuner:
        yield tr

# usage it within a loop ...
for tr in instance.getTribuner():
    # process tr

这将返回一个包含 self.__Tribuner 中所有成员的列表。

旁注:关于Python中的getter和setter,请参阅这篇文章:

Try:

def getTribuner(self):
    return self.__Tribuner

If don't want to return the list at once, you can use a generator, like so:

def getTribuner(self):
    for tr in self.__Tribuner:
        yield tr

# usage it within a loop ...
for tr in instance.getTribuner():
    # process tr

This will return a list with all members in self.__Tribuner.

Sidenote: Concerning getters and setters in python, see this article:

故事↓在人 2024-10-15 11:05:22
class Stadion(object):
    def __init__(self, tribuner):
        self.tribuner = tribuner

在您的代码中,将 stadion.GetTribuner() 替换为 stadion.tribuner。将 stadion.SetTribuner(t) 替换为 stadion.tribuner = t。要迭代 tribuner,请使用 for x in stadion.tribuner: 现在您正在使用 Python 而不是 Java 进行编程。

class Stadion(object):
    def __init__(self, tribuner):
        self.tribuner = tribuner

In your code, replace stadion.GetTribuner() with stadion.tribuner. Replace stadion.SetTribuner(t) with stadion.tribuner = t. To iterate over tribuner, use for x in stadion.tribuner: Now you're programming in Python rather than in Java.

一百个冬季 2024-10-15 11:05:22

来自 UserList.UserList 的子类,这一切变得更加容易。 :) 或者直接从列表中。或者更好地这样做:

class Stadion:
    def __init__(self,):
        self.tribuner=[]

完成。这就是你所需要的。您想要添加一个 SitteTribun:

stadion = Stadion()
stadion.tribuner.append(SitteTribute())

获取所有 Tribuner:

stadion.tribuner

循环遍历它们:

for tribun in stadion.tribuner:
    whatevah()

计数:

len(stadion.tribuner)

是。 Python 真的就是这么简单。 :-)

Subclass from UserList.UserList, and all this get's much easier. :) Or directly from list. Or even better do this:

class Stadion:
    def __init__(self,):
        self.tribuner=[]

Done. Thats all you need. You want to add a SitteTribun:

stadion = Stadion()
stadion.tribuner.append(SitteTribute())

Getting all the Tribuner:

stadion.tribuner

Looping over them:

for tribun in stadion.tribuner:
    whatevah()

Counting:

len(stadion.tribuner)

Yes. Python really is that easy. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文