[Tutor] Side effect and return value
Mats Wichmann
mats at wichmann.us
Fri Sep 20 11:31:23 EDT 2024
On 9/19/24 18:06, dn via Tutor wrote:
> Key Characteristics of Functional Programming
>
> 1 Pure Functions: A pure function is one where the output is determined
> solely by its input values, without observable side effects. This means
> that calling a pure function with the same arguments will always produce
> the same result, and it does not alter any external state or interact
> with outside systems (e.g., no modifying global variables or performing
> I/O operations).
>
> 2 Avoiding Side Effects: Functional programming aims to minimize side
> effects, which are changes in state that occur outside a given
> function's scope. By avoiding side effects, programs become easier to
> understand, test, and debug. This is because functions behave
> predictably and independently of the program's state.
>
> 3 Immutability: Data is immutable in functional programming, meaning
> once a data structure is created, it cannot be changed. Instead, new
> data structures are created when modifications are needed. This
> immutability helps prevent unintended side effects.
I think this is a clear example that while Python can be used for some
level of functional programming it is not designed as such. Many
builtin Python data structures, and thus many "works like"
implementations that take advantage of the familiarity with those
basics, are mutable, and their methods often have side effects AND
return values. Consider the pop() method of a dict:
pop(...) method of builtins.dict instance
D.pop(k[,d]) -> v, remove specified key and return the
corresponding value.
More information about the Tutor
mailing list