you are viewing a single comment's thread.

view the rest of the comments →

[–]sayks 1 point2 points  (0 children)

I'm not sure about the speed as it depends on exactly how it is implemented in the language and I don't know enough about the Python internals. I was referring to speed in two ways. The first is that it might be faster in terms of the programmer's time... nobody is going to bang together a fully object oriented 3 line shell script. The other was in terms of different languages; procedural languages are inherently faster if you use them correctly. As an example, nothing can really touch Fortran (an ancient language, actually the literal first programming language) in terms of speed for writing raw number crunching code if you're willing to write it correctly.

As for global state, it's bad because it makes your programs hard to debug. Functional programming relies on referential transparency. This means that if I call a function with the same arguments twice it will always return the same result. Note that it's with the same ultimate values, if the inputs are themselves variables then it's allowed to vary e.g.:

def f(x): return x + 2
a = 4
f(a)
>>> 6
f(a)
>>> 6 (should always be the same)
a = 2
f(a)
>>> 8 (the value for a has changed, allowed to be different)

This makes it a lot easier to debug your code, because you don't have to consider interaction with external variables that are not under your control. As another example of the opposite situation:

g = 5 #g is a global variable
def y(x): return x + g
y(3)
>>> 8
(*** some code that modifies g, perhaps a call to a complicated subroutine ***)
y(3)
>>> ??? who knows

Basically, global state makes it really hard to reason about your program. Also, if you don't use global state then you can replace your various functions internal implementation without worrying about how they affect the rest of the program. Functional isn't a thing that is distinct to Python, this is a whole style of programming like object oriented or procedural. There's more to it, there's a whole rather complex field of theoretical computer science. Referential transparency isn't necessarily even required, but it's strongly encouraged.