-1
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.

Sumit S Chawla
  • 3,180
  • 1
  • 14
  • 33
  • If you want give your object a printable representation, most cases you want to use `__repr__` instead of `__str__`. Related: https://stackoverflow.com/questions/1436703/what-is-the-difference-between-str-and-repr – Tzane May 13 '22 at 06:14
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 13 '22 at 06:58

1 Answers1

0

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.

Ellian Carlos
  • 26
  • 1
  • 3
  • So you mean if I want to make the object readable I should specify the index,crct? Also why is it that addresses get printed when you try to print an array of objects? is it some fxnality of python? – shubhang kulkarni May 13 '22 at 05:34
  • Yes, to make it readable you should especify the object. Python prints the addresses for an array of objects because that is what is stored in the memory, this is kind of a heritage from C, where the array stores addresses instead of objects. – Ellian Carlos May 13 '22 at 14:40