(toiminnot)

hwechtla-tl: How to write functional python: viime muutokset

[...]

# Don't modify data structures. No .append(). # Don't modify variables. You ''can'' assign to a variable that doesn't exist yet. # If you break the above rules, do it only within a function. Especially, don't ever modify data that you got as an argument, gave as an argument, or got as a return value of a subcall. You can, however, return values that you modified earlier, and give as argument values that you modified earlier. # Don't refer inherently stateful data structures (such as iterators) multiple times. The easiest way to achieve this is not to give them a name (i.e. not assign them anywhere). If you need to put them into data structures, convert them to something rereadable, such as list.

[...]

Here are the non-destructive variants (returning new data structures instead of modifying the existing one) of the builtin operations: {{{ Destructive Non-destructive ls.append(x) ls + [x] ls.clear() [] ls.copy() ls ls.extend(ls2) ls + ls2 ls.pop(i) # or del ls[i] ls[:i] + ls[i+1:] ls.reverse() reversed(ls) ls.sort() sorted(ls) ls.insert(i, x) [*ls[:i], x, *ls[i:]] ls[i] = x [*ls[:i], x, *ls[i+1:]] ls[i:j] = ls2 ls[:i] + ls2 + ls[j:] }}}

[...]


(viimeksi muutettu 18.08.2022 11:15)