'What is the proper way to create a class instance with an empty dictionary and/or list? [closed]
This answer suggests I am creating my empty dictionary the correct way.
class Board:
def __init(self):
self.pot = 0
self.activePlayer = 1
self.activePlayers = 4
self.passedPlayers = 0
self.firstHand = True
self.lastCardsPlayedList = []
self.lastCardsPlayedDict = {}
def playCard(self, cardInt, cardPic):
self.lastCardsPlayedDict[cardInt] = cardPic
self.lastCardsPlayedList.append(cardInt)
self.lastCardsPlayedList.sort()
I create an instance of my class
b = Board()
but when I go to call it...
b.playCard(1, cardPic)
I get the error:
AttributeError: 'Board' object has no attribute 'lastCardsPlayedDict'
Solution 1:[1]
Silly Mistake...your init method is not defined correctly.
Here is the corrected code.
class Board:
def __init__(self):
self.pot = 0
self.activePlayer = 1
self.activePlayers = 4
self.passedPlayers = 0
self.firstHand = True
self.lastCardsPlayedList = []
self.lastCardsPlayedDict = {}
def playCard(self, cardInt, cardPic):
self.lastCardsPlayedDict[cardInt] = cardPic
self.lastCardsPlayedList.append(cardInt)
self.lastCardsPlayedList.sort()
b = Board()
b.playCard(1, 'cardPic')
print(b)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rama |
