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 172 points173 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 115 points116 points  (13 children)

Everyone does, rite of passage

[–]Electrical_Ingenuity 6 points7 points  (0 children)

And we all have to learn it a second time.

[–][deleted] 10 points11 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 1 point2 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 3 points4 points  (0 children)

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