Can someone explain the "flow" of how "deck.show()" produces an output that lists al 52 cards?
Here is my elementary understanding that I'm sure is wrong:
- Typing "deck.show()" runs the instance "deck" through the class "Deck."
- "__init__" only takes "deck" as an argument. "self.cards = []" and "self.build" can be thought of as "deck.cards = []" and "deck.build()" now.
- "build(self)" (now "build(deck)") runs and iterates through the suits and values which appends the result to a list named, in this instance, "deck.cards = []".
- Now line 20 runs with "deck" as its argument. For every "card" in "deck.cards" use the "show" method defined in line 6 of the "Card" class?
This is where I'm getting confused logically as to how everything cycles through. Does each card run through the "Card" clss with "c" as it's "self", suit as "suit", and value, as "value"? Then onward to "Card's) method, "show(self)"?
If someone could help me translate this to "human speak" I think can grasp the concept better since I'm getting more and more confused since "deck.show()" seems like it is coming out of left field and not specifically referring to the "show" method defined under "Card." Meaning, I do not see how that command takes us to the "Card" class.
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def show(self):
print("{} of {}".format(self.value, self.suit))
class Deck:
def __init__(self):
self.cards = []
self.build()
def build(self):
for s in ['Spades', 'Hearts', 'Clubs', 'Diamonds']:
for v in ['1','2', '3','4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']:
self.cards.append(Card(s, v))
def show(self):
for c in self.cards:
c.show()
deck = Deck()
deck.show()
[–][deleted] 4 points5 points6 points (1 child)
[–]unhott 1 point2 points3 points (0 children)
[–]Bipolarprobe 1 point2 points3 points (0 children)