【问题标题】:Object Oriented Programming: Problem with self in the Constructor, Python 3面向对象编程:构造函数中的 self 问题,Python 3
【发布时间】:2021-07-30 07:49:38
【问题描述】:

我目前正在尝试学习 python 并尝试理解类,但我遇到了一个奇怪的问题。

class card:
    def __init__(self, s, num, name):
        self.suit = s
        self.number = num
        self.name = name

class aceCard(card):
    def __init__(s):
        card.__init__(s, 0, "ace of " + s)
        value1 = 1
        valye11 = 11

class numCard(card):
    def __init__(s, num):
        name = num, " of ", s
        card.__init__(s, num, name)
        value = num

class faceCard(card):
    def __init__(s, num):
        if (num == 11):
            name = "jack of " + s
        elif (num == 12):
            name = "queen of " + s
        elif (num == 13):
            name = "king of " + s
        card.__init__(s, num, name)
        value = 10

class suits:
    cards = []
    def __init__(s):
        cards = [aceCard.__init__(s)]
        n = 1
        while n<11:
            cards.append(numCard.__init__(s, n))
            n+=1
        while n<14 and n>10:
            cards.append(faceCard.__init__(s, n))
            n+=1
        
class deck:
    adeck = []
    def __init__():
        adeck = [suits.__init__("clubs"), suits.__init__("diamonds"), suits.__init__("hearts"), suits.__init__("spades")]
    
    def print():
        for suitses in adeck:
            for cardses in suitses:
                print(cardses.name)
            
                
        
deck1 = deck.__init__()
deck1.print()

来自 Spyder IDE 的错误消息是

runfile('###', wdir='###')
Traceback (most recent call last):

  File "###", line 55, in <module>
    deck1 = deck.__init__()

  File "###", line 46, in __init__
    adeck = [suits.__init__("clubs"), suits.__init__("diamonds"), suits.__init__("hearts"), suits.__init__("spades")]

  File "###", line 34, in __init__
    cards = [aceCard.__init__(s)]

  File "###", line 10, in __init__
    card.__init__(s, 0, "ace of " + s)

TypeError: __init__() missing 1 required positional argument: 'name'

让我困惑的部分是解释器希望 self 像普通参数一样被使用,而不是被跳过。请帮忙。

编辑: 我搜索的任何内容似乎都无法解决我的问题。我的问题是,我要检查的所有内容都告诉我使用 self 作为参数,并且在代码中我要像 java 中的“this”一样使用它。但是,当我调用构造函数时,我将忽略 self 作为第一个参数,只使用后面的参数。我错过了什么问题?

【问题讨论】:

  • 你在哪里困惑?错误信息非常清楚。您将card.__init__ 定义为需要四个参数(请参阅您的代码)。当您在第 10 行调用代码时,您只提供了三个参数。您必须决定初始化方法将如何工作。
  • 另请注意,这不是创建类实例的常用方法:在每个调用中,您创建一个 card 并忽略您创建的对象 --您无法再次引用该卡。我建议(1)您重复有关类和实例的教程以了解常见用法; (2) 你使用更好的编程实践:在编写更多代码之前测试你的代码的一小部分以确保它们工作。您已经发布了大约 50 行代码,其中有几个需要修复的编程违规行为。
  • @Prune “关于类和实例的教程以学习常用用法”的任何好的推荐
  • @Prune 您实际上会使用 3 个参数调用父初始化,但 python 使用 super() 首先构造父实例(使用 __new__),然后将该实例提供给 __init__ .

标签: python oop constructor spyder blackjack


【解决方案1】:

您的代码有几个问题,但问题的根源在于您不知道如何构造父实例。

Python 使用super() 来做到这一点:

class Card:
    def __init__(self, s, num, name):
        self.suit = s
        self.number = num
        self.name = name

class AceCard(Card):
    def __init__(self, s):
        super().__init__(s, 0, "ace of " + s)
        # This does nothing. Just creates local variables which are discarded
        # once the function ends.
        value1 = 1
        valye11 = 11

我可以推荐 this tutorial 作为 Python 的 OOP 实现的一个很好的介绍。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多