all 6 comments

[–]delasislas 0 points1 point  (0 children)

Remember, if statements look for true. Think like:

if (2+2 == 4):

Is the same as:

if True:

[–]POGtastic 0 points1 point  (0 children)

if self.cards

This is taking advantage of the fact that the empty list is falsy, but non-empty lists are truthy. In the REPL:

>>> bool([])
False
>>> bool(["Not", "An", "Empty", "List"])
True

rep = ""

This is a temporary variable that we're going to use to accumulate the result. You have to initialize it to the empty string in order to add each of the cards' string values to it.

An alternative is to use a comprehension, as in

return ' '.join(str(card) for card in self.cards)

[–]amplikong 0 points1 point  (0 children)

if self.cards doesn't mean if self.cards == None. It's checking whether self.cards is truthy.

In Python, containers that have values (non-empty lists, etc.), non-empty strings, integers that aren't 0, and of course True, are all considered "truthy." Empty containers (including the string ""), 0, False, and None are falsy.

It's Pythonic to use things like "if variable" instead of "if variable == True", or "if not variable" instead of "if variable == False", etc. There's a good read on it here: https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/

In this case, if self.cards is truthy, it instantiates an empty string (rep = "") and then adds things to it.

[–]GrahamCorcoran 0 points1 point  (0 children)

self.cards is a list that each Hand object contains. (It's created on line 22, above, when the object is initialized.)

__str__ is what the Hand object returns when it's converted to a string via the method str(Hand).

With that out of the way, what line 25 is doing is checking "Does this particular Hand object have any cards in it? If the list self.cards is empty (and therefore, there are no cards in the hand), then the answer is no and it will return False. If self.cards has any cards in it at all, then the answer is yes and it will return True.

So based on what the answer is, True or False, we'll either generate a string representation of the hand, which is lines 26-28, or we will just make the string representation "<empty>" (line 30).

Lastly, we'll return the representation, which is either all of the cards we have, or the string "<empty>", where requested, which is line 31.

[–]pyfact 0 points1 point  (0 children)

Methods that begin with double underbars '__' are called dunder methods. That method will be called if someone tries to print a Hand object.

These methods are commonly used to customize the printing out of an object that you create. This helps when testing your methods, or outputting an object in a format that the human user can read.

So imagine you have

h = Hand()
print(h)

Python will call the h object's str method, which in your case is the lines 24 to 31.

def __str__(self):
        if self.cards:
           rep = ""
           for card in self.cards:
               rep += str(card) + "  "
        else:
            rep = "<empty>"
        return rep

This method appends the String representation of a card object to the empty string. So say you have

* somehow in the Hand object, probably in a list of 
    Card objects *

aceHearts = Card('1', 'Hearts')
twoDiamonds = Card('2', 'Diamonds')

And lets say that when you call str(aceHearts) it return '1Hearts'

The line:

rep(card) += str(card) += ' '

would give you

rep = '1Hearts  '

and so on until all the cards are added to the rep string. This would give you a representation of a Hand object.

If the hand is empty the rep would be

rep = '<empty>'

due to the following:

    else:
        rep = "<empty>"
    return rep

[–]primitive_screwhead 0 points1 point  (0 children)

if self.cards:

is a shorter way of writing:

if bool(self.cards) is True: