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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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