【问题标题】:Python Classes and the self [duplicate]Python类和自我[重复]
【发布时间】:2021-08-30 18:00:09
【问题描述】:

我有一个关于 Classes 和 self. 的初学者小问题 我的函数 checkAbonnement 现在看起来像这样:

    def checkAbonnement(self):
    # Create Labels and Buttons
       self.text = Label(text="Choose the Subscription you got")
       self.text.grid(row=1, column=0)
       self.aboButton = Button(window, text="No Subscription", command=lambda:[self.setAbonnement(1), self.checkGutschein()])
       self.aboButton.grid(row=2, column=0)
       self.aboButton_2 = Button(window, text="Silver Subscription", command=lambda:[self.setAbonnement(2), self.checkGutschein()])
       self.aboButton_2.grid(row=3, column=0)
       self.aboButton_3 = Button(window, text="Gold Subscription", command=lambda:[self.setAbonnement(3), self.checkGutschein()])
       self.aboButton_3.grid(row=4, column=0)
       self.aboButton_4 = Button(window, text="Platinum Subscription", command=lambda:[self.setAbonnement(4), self.checkGutschein()])
       self.aboButton_4.grid(row=5, column=0)

如你所见,里面有一堆self....

我现在的问题是:除了使用 self. 之外,是否需要它们或者是否有更好的选择向我的班级声明它们

class ScooTeq:

【问题讨论】:

  • self 本身并不重要。要理解的是为什么您的方法有一个参数(通常称为self,但您可以使用您喜欢的任何名称)在您调用该方法时似乎不带参数。跨度>

标签: python class tkinter declaration


【解决方案1】:

如果您希望这些属性在ScooTeq 的实例中是唯一的,那么您必须使用self。我同意这可能很乏味,但这是为了避免歧义。这个here有更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 2015-08-07
    • 2016-01-25
    相关资源
    最近更新 更多