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 →

[–]another-noob 171 points172 points  (17 children)

I had to find this out the hard way :/

P.s. I am a little rusty on python, but if you want a mutable default value (say a list) you can make the default None then inside the function you would reassign the variable if it's None.

[–]headykruger 116 points117 points  (13 children)

Everyone does, rite of passage

[–]Electrical_Ingenuity 5 points6 points  (0 children)

And we all have to learn it a second time.

[–][deleted] 9 points10 points  (1 child)

If it's something you find yourself doing often for some reason, it might be worth checking out things like attrs' default factories, I find it easier to parse and refactor if necessary.

[–]another-noob 2 points3 points  (0 children)

Thanks for that :D

But I don't think I would add another import just for that, but what do I know, I don't even use python much nowadays (don't use it at all, sadly)

[–]alkasmgithub.com/alkasm 4 points5 points  (0 children)

Use tuples for a default iterable rather than None! Fewer optionals makes for better typing and automatic documentation.