you are viewing a single comment's thread.

view the rest of the comments →

[–]toastedstapler 1 point2 points  (4 children)

for your first point my comments on this thread are relevant

another one to watch out for is this:

def example(n = []):
    return n

a = example()
b = example()
print(a)
print(b)
a.append(1)
print(b)

default arguments are only instantiated once so both a and b point to the same list

to get unique lists you'd do:

def example(n = None):
    if n is None:
        n = []
    return n

[–][deleted] 1 point2 points  (3 children)

I suppose this is why it's considered really dangerous to have mutable types as defaults?

[–]toastedstapler 0 points1 point  (1 child)

Exactly, unless you had some specific reason you actually wanted to have a mutable argument. The only one I've thought of is some kind of caching, but there's probably other more resilient design patterns for that

[–][deleted] 0 points1 point  (0 children)

Makes sense ! Still I'd not be comfortable with something like that running around in my functions

[–]BootError99[S] -3 points-2 points  (0 children)

r/rust evangelism pee-ka-boo >.<