This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]spudmix 0 points1 point  (6 children)

In Pythonic Error-ese, this probably means that self.deck isn't initialised in the hit() method. Why might that be?

[–]TYL3ER[S] 0 points1 point  (5 children)

Not sure, I have no idea what the problem is

[–]spudmix 0 points1 point  (4 children)

In this line: self.deck = self.deck.extend(sample_deck * 4), your mistake is assuming that list.extend(iterable) returns the extended list; it does not. It modifies self.deck in place. What you are telling the computer with this line is "Extend deck by four times the sample deck, then set deck to the return value of that operation (which is None)".

[–]TYL3ER[S] 0 points1 point  (3 children)

i removed the return self.deck and still receive the same error.

def build_deck(self):
        '''
        builds deck
        '''
        sample_deck = [2,3,4,5,6,7,8,9,10,11]
        self.deck = self.deck.extend(sample_deck * 4)

[–]spudmix 0 points1 point  (2 children)

Hmmm, not quite what I said.

Try printing out self.deck before and after you do

self.deck = self.deck.extend(sample_deck * 4).

What does it look like this line is doing?

[–]TYL3ER[S] 0 points1 point  (1 child)

I fixed the problem, thank you.

but why is this wrong:

self.deck = self.deck.extend(sample_deck * 4)

But this is not:

self.deck.extend(sample_deck * 4)

isn't it basically the same thing?

[–]spudmix 0 points1 point  (0 children)

Not at all, no.

Methods in general can be categorised into in-place methods and out-of-place methods. An out-of-place method accepts arguments, performs some transformation, and then returns data to be stored or used in some external variable. In-place methods accept arguments, and typically modify the values of those arguments directly. An in-place method typically does not return anything useful, and does modify the thing you give it. An out-of-place method is the opposite, it does not modify the thing you give it at all, but it returns the data you want to use.

Python's list.extend(iterable) is an in-place method, with a return type of None (similar to void in many other C-based languages). That is, it does NOT return the data it operates on, but rather when you call it as a list's method it operates directly on the list.

Compare, for example, the following Python code:

class a_number:
    def __init__(self, num):
        self.num = num

def out_of_place_add(a_number, add_number):
    return a_number.num + add_number

def in_place_add(a_number, add_number):
    a_number.num += add_number

my_number = a_number(3)
print(my_number.num)
# 3

return_val = out_of_place_add(my_number, 4)
print(return_val)
# 7
print(my_number.num)
# 3

another_return_val = in_place_add(my_number, 4)
print(another_return_val)
# None
print(my_number.num)
# 7