Dec-30-2021, 10:16 AM
Hi Team Python!
I’m trying to build a deck of 52 playing cards using OOP and list comprehension.
Here are some scripts:
Here is another attempt, this time with the list comprehension embedded within the class
In my next iteration of my script I attempted to swap out the hard coded class attributes and try passing them in during instantiation like this:
I’m stabbing in the dark at this point. Can any one shed elaborate on what the tracebacks are trying to convey and what exactly I am doing wrong in each script iteration above?
I’m trying to build a deck of 52 playing cards using OOP and list comprehension.
Here are some scripts:
class Card: def __init__(self, suits, value): self.suits = ["Hearts", "Diamonds", "Clubs", "Spades"] self.value = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] class Deck: pass cards = [Card(self.value, self.suits) for self.value in range(1, 14) for suit in self.suits] print(cards)The traceback in my shell reads:
Error:$ python card-deck-exercise.py
Traceback (most recent call last):
File "/home/<user>/dev/projects/python/2018-and-2020/The-Modern-Python-3-Bootcamp-Colt-Steele-on-Udemy/OOP/card-deck-exercise.py", line 10, in <module>
cards = [Card(self.value, self.suits) for self.value in range(1, 14) for suit in self.suits]
File "/home/<user>/dev/projects/python/2018-and-2020/The-Modern-Python-3-Bootcamp-Colt-Steele-on-Udemy/OOP/card-deck-exercise.py", line 10, in <listcomp>
cards = [Card(self.value, self.suits) for self.value in range(1, 14) for suit in self.suits]
NameError: name 'self' is not defined
This error tells me that Python doesn’t like the way I am calling or referring to the class attributes self
when I attempt to instantiate at line 10. So I tried removing self
which returned a name error saying that suits
is not defined.Here is another attempt, this time with the list comprehension embedded within the class
__init__
method and called within a __repr__
method:class Card: def __init__(self, suits, value, cards): self.suits = ["Hearts", "Diamonds", "Clubs", "Spades"] self.value = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] self.cards = [Card(value, suits) for value in range(1, 14) for suit in suits] def __repr__(self): return f"This should be your list of cards (raw): {self.cards}." class Deck: pass j = Card() print(repr(j))The traceback now shows:
Error:$ python card-deck-exercise.py
]Traceback (most recent call last):
File "/home/gnull/dev/projects/python/2018-and-2020/The-Modern-Python-3-Bootcamp-Colt-Steele-on-Udemy/OOP/card-deck-exercise.py", line 14, in <module>
j = Card()
TypeError: __init__() missing 3 required positional arguments: 'suits', 'value', and 'cards'
This error tells me that when the Card
class is called, Python is expecting positional arguments to be initialized or passed in. However as I understand this class, the attributes are ‘hard coded’ into the class definition so when instantiated, arguments shouldn’t be required to be passed in. So I am not sure why I am getting this particular traceback.In my next iteration of my script I attempted to swap out the hard coded class attributes and try passing them in during instantiation like this:
class Card: def __init__(self, suits, value, cards): self.suits = suits self.value = value self.cards = cards def __repr__(self): return f"This should be your list of cards (raw): {self.cards}." class Deck: pass j = Card(self,["Hearts", "Diamonds", "Clubs", "Spades"],["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"], [Card(value, suits) for value in range(1, 14) for suit in suits]) print(repr(j))The traceback now reads:
Error:$ python card-deck-exercise2.py
Traceback (most recent call last):
File "/home/gnull/dev/projects/python/2018-and-2020/The-Modern-Python-3-Bootcamp-Colt-Steele-on-Udemy/OOP/card-deck-exercise2.py", line 14, in <module>
j = Card(self,["Hearts", "Diamonds", "Clubs", "Spades"],["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"], [Card(value, suits) for value in range(1, 14) for suit in suits])
NameError: name 'self' is not defined
With classes, the self
argument is always a convention. It’s not really supposed to be defined. It’s just always included. And in my case, it’s included both in the class definition and when I attempt to instantiate. I tried removing the self
argument from instantiation which then says suits
is not defined.I’m stabbing in the dark at this point. Can any one shed elaborate on what the tracebacks are trying to convey and what exactly I am doing wrong in each script iteration above?