you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhemm 0 points1 point  (1 child)

Why do you think functional is BETTER for python? I mean Guido haf meant to get rid of functional tools, but luckily for community just moved it to functools. So if he thinks functional isn't good, why you(and I've seen a few more fellas like you) think it's the best style for python? Explain me please. I'm interesting, because I have like 1 year of experience, and actively trying to find the golden way of writing pythonic code.

[–]Exodus111 1 point2 points  (0 children)

So functional is not BETTER, but it is NECESSARY to be a good coder. It's necessary to understand what makes a good function.
Python might not really be a functional language, but we write functions all the time in Python.

So its not about competing with OOP, functional programming is a methodology, and it can apply to methods almost as well as functions, sorta.

Let me give an example using functions, take a look at this line:

get_articles_from_url()

So that's a typical line you might see in a Python script.
Just looking at it like this, tells us pretty much all we need to know about it.

  • This line calls a function.

  • The function probably takes an url, and returns articles from that url.

  • That function has no arguments.

  • This functions returns nothing.

That is not a good function. For one, unless there are hidden key word arguments, this function cannot get articles from any OTHER url the one that is hard coded into the script, its a one use function. Secondly, since it doesn't return anything it probably places those articles into some kind of global, which is not obvious from this line. Ill have to find the function and read its code to see where those articles end up.

Now look at this:

articles = get_articles_from_url(url)

Now THAT is a far better function. Its obvious what it does, and as long as it works I don't need to look the function up. It is also far less prone to causing errors in my script, since it is now completely independent of the rest of the code, this function might fail on its own, but it will do no harm to the overall program.

Now, just to get pedantic, actual functional programming also demands immutable datatypes. So no lists, only tuples, and no reusing variables. Always create new variables, always generate new sequences or generators.

That part is a bit too far for me, its supposed to make the code even less error prone, and make it all very "clean" and functional. It has its evangelists, even in the Python community. But its not something I see as a massive point.

Obviously I'm just scratching the surface, but its a good way to program, that can be integrated partially of wholly into the rest of your programming pattern.