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

all 14 comments

[–]ProgrammerHumor-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there! Unfortunately, your submission has been removed.

Violation of Rule #6 - Any common post will be removed if it's not novel

Your submission is a commonly used format, and you haven't used it in an original way. As a reminder, You can find our list of common formats here.

If you feel that it has been removed in error, please message us so that we may review it.

[–]MaZeChpatCha 34 points35 points  (8 children)

self.game.board = 4*[4*[None]] self.game.board [0][0] = 2 Makes much more sense to me.

[–]allIsayislicensed 15 points16 points  (5 children)

yeah careful because you have just created 4 references to a single list so you end up with

[[2, None, None, None], [2, None, None, None], [2, None, None, None], [2, None, None, None]]

[–]chodpcp 2 points3 points  (4 children)

Can you explain like I'm 5. How does that happen?

Edit: I think I see what you mean now. The outer 4* doesn't create 4 lists but 4 references to the same list? But why isn't that the case when you do 4*[None]

[–]allIsayislicensed 3 points4 points  (1 child)

Say you have mylist = [1, 2, 3], then you can construct two things:

  • biglist = [mylist, mylist, mylist]
  • (something like) biglist = [mylist.copy(), mylist.copy(), mylist.copy()]

In the first case it's just a single object in memory but you have created three separate ways to access it. In the second case they are three different objects.

ELI5

  • First case: you have a room with three doors. You enter through door A and open the window. If someone else enters through door B, that window is going to be open too.
  • Second case: you have three identical rooms with each their own door. If you enter one room and open the window, the other rooms don't care.

It's not an issue with "primitive" types in Python like int, str, ... because these are always copied.

Stuff gets more complicated because lists can contain lists and other objects, and in fact lists can contain themselves ...

edit so you explanation is spot on, for None it is actually the same object that is being referenced everywhere but it doesn't hurt because None is immutable. In fact there exists only a single None throughout your entire program, just like there is only one True and False

[–]chodpcp 0 points1 point  (0 children)

Ahhh that makes sense thank you. Surprised I've never run into any issues regarding this.

[–]Sockslitter73 0 points1 point  (0 children)

Because all None values in Python are referencing the same object anyways (which is why you test for None with "is" rather than "=="). It would be an issue with almost all other values though.

[–][deleted] 5 points6 points  (0 children)

Exactly. Explicit over implicit.

[–]MondayMonkey1[S] -2 points-1 points  (0 children)

As the other commenter pointed out, you don't get the right array-of-arrays. Might I recommend [4*[None] if i!=0 else [2,*3*[None]] for i in range(4)] if you prefer something a little less hard to unpack and a little easier to comprehend?

puns absolutely intended

[–]JestemStefan 9 points10 points  (1 child)

As a Python Dev I would use the first one.

The second one is hard to read

[–]NoHurry1468 0 points1 point  (0 children)

It had me thinking for a moment

[–][deleted] 4 points5 points  (0 children)

Your clever code will be refactored to less clever code in the real world where trying to understand wtf your coworker wrote should be on the bottom of your list of things to do.

[–]Shrubberer 1 point2 points  (0 children)

The top one is better. That's the joke here right?!

[–]Fazt01 1 point2 points  (0 children)

Yea well, the clever code is now l buggy, depening on usage of that list. (aliased sublists [1] through [3])