了解类内的 python 变量范围

发布于 2024-09-24 12:06:29 字数 614 浏览 2 评论 0原文

我试图在类中定义一个变量,然后可以从该类中的函数访问/更改该变量。

例如:

class MyFunctions():
    def __init__( self):
        self.listOfItems = []

    def displayList( self):
        """Prints all items in listOfItems)"""
        for item in self.listOfItems:
            print item

    def addToList(self):
        """Updates all mlb scores, and places results in a variable."""
        self.listOfItems.append("test")

f = MyFunctions()
f.addToList
f.displayList

这应该为我输出列表中的所有项目,但它什么也不显示。我假设发生这种情况是因为我没有正确设置变量的范围。我希望能够从 MyFuctions 中的所有函数中访问和更改 listOfItems。

我已经尝试解决这个问题几个小时了,所以任何帮助将不胜感激。

I am trying to define a variable in a class that then can be accessed/changed from functions within that class.

For example:

class MyFunctions():
    def __init__( self):
        self.listOfItems = []

    def displayList( self):
        """Prints all items in listOfItems)"""
        for item in self.listOfItems:
            print item

    def addToList(self):
        """Updates all mlb scores, and places results in a variable."""
        self.listOfItems.append("test")

f = MyFunctions()
f.addToList
f.displayList

This should output all of the items in the list for me, but instead it displays nothing. I am assuming this is occuring because I did not setup the scope of the variables correctly. I want to be able to access and change listOfItems from within all of the functions in MyFuctions.

I have been trying to figure this out for a few hours now, so any help would be greatly appreciated.

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

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

发布评论

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

评论(1

归属感 2024-10-01 12:06:29

f.addToListf.displayList 不会分别调用 addToListdisplayList 方法。它们只是对方法本身进行求值(在本例中绑定到对象f)。添加括号来调用方法,就像程序的更正版本中一样:

class MyFunctions():
    def __init__( self):
        self.listOfItems = []

    def displayList( self):
        """Prints all items in listOfItems)"""
        for item in self.listOfItems:
            print item

    def addToList(self):
        """Updates all mlb scores, and places results in a variable."""
        self.listOfItems.append("test")

f = MyFunctions()
f.addToList()
f.displayList()

这与 Ruby 不同,Ruby 不需要括号来调用方法(除了在某些情况下消除歧义)。

将以下内容添加到程序末尾是有启发性的:

print type(f.addToList)

这将输出类似以下内容:

<type 'instancemethod'>

证明这是方法引用而不是方法调用。

f.addToList and f.displayList do not invoke the methods addToList and displayList respectively. They simply evaluate to the method (bound to the object f in this case) themselves. Add parentheses to invoke the methods as in the corrected version of the program:

class MyFunctions():
    def __init__( self):
        self.listOfItems = []

    def displayList( self):
        """Prints all items in listOfItems)"""
        for item in self.listOfItems:
            print item

    def addToList(self):
        """Updates all mlb scores, and places results in a variable."""
        self.listOfItems.append("test")

f = MyFunctions()
f.addToList()
f.displayList()

This is in contrast to Ruby which does not require parentheses for method invocation (except to eliminate ambiguity in certain cases).

It is instructive to add the following to the end of your program:

print type(f.addToList)

This will output something like the following:

<type 'instancemethod'>

demonstrating that this is a method reference and not a method invocation.

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