'Python-Classes and Objects
import random
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8,'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13,'Ace':14}
class Card:
def __init__(self,suit,rank):
self.suit=suit
self.rank=rank
self.value=values[rank]
def __str__(self):
return self.rank + 'of' + self.suit
class Deck:
def __init__(self):
self.all_cards=[]
for suit in suits:
for rank in ranks:
created_card=Card(suit,rank)
self.all_cards.append(created_card)
def shuffle(self):
random.shuffle(self.all_cards)
def deal_one(self):
return self.all_cards.pop()
new_deck=Deck()
print(new_deck.all_cards) # a
print(new_deck.all_cards[0]) # b
Here in the above code when I tried to print all the 52 card objects without mentioning any index as shown in a) I got the following output-:
[<main.Card object at 0x00000259B1890E80>, <main.Card object at 0x00000259B18907C0>, <main.Card object at 0x00000259B1890AC0>, <main.Card object at 0x00000259B1890490>, <main.Card object at 0x00000259B1890730>, <main.Card object at 0x00000259B1890970>, <main.Card object at 0x00000259B1890FD0>, <main.Card object at 0x00000259B1890CA0>, <main.Card object at 0x00000259B1890400>, <main.Card object at 0x00000259B1890A30>, <main.Card object at 0x00000259B1890A00>, <main.Card object at 0x00000259B1890F10>, <main.Card object at 0x00000259B1890250>, <main.Card object at 0x00000259B1890A60>, <main.Card object at 0x00000259B18902B0>, <main.Card object at 0x00000259B1890310>, <main.Card object at 0x00000259B1890A90>, <main.Card object at 0x00000259B18964C0>, <main.Card object at 0x00000259B1896580>, <main.Card object at 0x00000259B1896520>, <main.Card object at 0x00000259B18966A0>, <main.Card object at 0x00000259B1896760>, <main.Card object at 0x00000259B18967C0>, <main.Card object at 0x00000259B18968B0>, <main.Card object at 0x00000259B1896910>, <main.Card object at 0x00000259B1896640>, <main.Card object at 0x00000259B18969A0>, <main.Card object at 0x00000259B1896A60>, <main.Card object at 0x00000259B1896A30>, <main.Card object at 0x00000259B1896AC0>, <main.Card object at 0x00000259B1896B50>, <main.Card object at 0x00000259B1896B20>, <main.Card object at 0x00000259B1896B80>, <main.Card object at 0x00000259B1896CA0>, <main.Card object at 0x00000259B1896D00>, <main.Card object at 0x00000259B1896D90>, <main.Card object at 0x00000259B1896E20>, <main.Card object at 0x00000259B1896EB0>, <main.Card object at 0x00000259B1896F10>, <main.Card object at 0x00000259B1896C40>, <main.Card object at 0x00000259B1896F40>, <main.Card object at 0x00000259B1896FA0>, <main.Card object at 0x00000259B18966D0>, <main.Card object at 0x00000259B1896BB0>, <main.Card object at 0x00000259B1896BE0>, <main.Card object at 0x00000259B18969D0>, <main.Card object at 0x00000259B1855400>, <main.Card object at 0x00000259B1855B20>, <main.Card object at 0x00000259B1855E50>, <main.Card object at 0x00000259B1855A90>, <main.Card object at 0x00000259B18558E0>, <main.Card object at 0x00000259B1855DC0>]
But when I tried to print by mentioning an index as shown in b) I got the following output:- TwoofHearts
Can anybody explain me the difference between the 2 outputs? As in I want to understand the concept behind this.
Solution 1:[1]
On the first print, you're trying to print a array of cards, that's what the Python interpreter knows, so it shows you their types and addresses.
On the second print, since you especified an object, it know what the object is and it tries to convert it to a printable type ( in this case, the string implemented on the __str__
method ).
The main key difference is that the interpreter knows how to look for a __str__
function given an object, but not given an array of objects.
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 | Ellian Carlos |