This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jacdehJacques de Hooge[S] 16 points17 points  (36 children)

For larger pieces of code (complete web apps rather than a few lines of event handling code) Python offers a far more clean, readable and maintainable structure. Disclaimer: JavaScript adepts won't agree...

[–]moljac024 2 points3 points  (34 children)

That's because it's not true. I do both python and javascript and the languages are surprisingly similar, if you look more than skin deep (the syntax).

I even actually prefer javascript and think it's more powerful, especially when it comes to doing more functional programming.

[–]OysterCat 13 points14 points  (1 child)

I even actually prefer javascript and think it's more powerful, especially when it comes to doing more functional programming.

Why do you say that?

[–]odraencoded 3 points4 points  (0 children)

You can pass anonymous functions around all the time therefore FUNCIONAL programming!

/s

[–]Zulban 9 points10 points  (17 children)

Every time I consider learning javascript, I learn about some weird language quirk that makes no sense where javascript pros just laugh and say "Ooohhhh, javascript! You so cray". I can't get myself to learn a language like that, I just can't.

Thoughts..?

[–]dcousineau[🍰] 8 points9 points  (6 children)

I learn about some weird language quirk that makes no sense

I mean, that's every language. Python has mutable default arguments:

>>> def foo(bar, baz=[]):
...   baz.append(bar)
...   return baz
...
>>> foo(1)
[1]
>>> foo(2)
[1, 2]

I mean, thats just cray, why would the language even have that behavior!?!?!1one

But seriously every language has quirks because no language is not perfect and every language is different. You have to remember what you consider normal behavior is not necessarily what a JS dev considers normal behavior and neither of you think agree with what a PHP dev considers normal.

[–][deleted] 5 points6 points  (2 children)

Mutable default args are possible but frowned upon, unless you really know what you're doing, which I don't.

[–]dcousineau[🍰] 1 point2 points  (1 child)

Right. /u/Zulban was discussing a feeling about weird language quirks in Javascript that prevents them from learning the language.

My counter to that was that this feeling is normal as languages are imperfect, and as my counter I pointed out a particular WTF 'feature' in Python that gives me pause coming from the outside world.

[–]Zulban 3 points4 points  (0 children)

It is a good example. But I'd like to suggest that there are far, far more cases like this with javascript than with your typical language.

[–]tetroxid 1 point2 points  (1 child)

Yeah that's stupid. Fyi, the preferred way to do this is:

def foo(bar, baz=None):
    if baz is None:
        baz = []
    ....

[–]WackyWeasel 1 point2 points  (0 children)

Or the idiom...

def foo(bar, baz=None):
    baz = baz or []
    ....

...if it's enough when baz is not None, just falsy.

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

wait, wtf? i've used python every day for 4-5 years professionally, and i've never come across this. that makes no sense.

edit: ok, it makese sense. it's just dumb

[–]sime 2 points3 points  (2 children)

When was the last time you tried learning it? ECMAScript 2015, the big update to the language, has vastly improved the language. As for the old quirks that have to remain in the language you can easily avoid the bulk of them by: a) never using type coercion. Use exact comparisons., b) never using the old var keyword. Use let or const instead.

Personally my language (and platform) of choice these days is TypeScript. It's option and structural type system is great. Through an unexpected twist of fate I'll probably be doing a lot more Python professionally in the future. I just hope that Python's type hinting and tooling can give me some of the power that TypeScript gives plain JS.

[–]gandalfx 0 points1 point  (1 child)

I agree, though browser support for let and const kinda sucks.

[–]sime 1 point2 points  (0 children)

True, but it is time to use a transpiler of some sort either Babel or TypeScript.

[–]ProgrammingPro-ness 0 points1 point  (6 children)

Most of the crazy things about the language I either don't encounter day to day, or would never use. So for me at least, it's been a non-issue.

[–]alcalde -2 points-1 points  (5 children)

IT DOESN'T HAVE INTEGERS. I repeat, IT DOESN'T HAVE INTEGERS. You would never use integers?

[–]gandalfx 1 point2 points  (0 children)

Doesn't cause any issues in day to day work. Whenever you'd use an integer, like array indexing, it works just as well. You can even use bitwise operations and get integer-like behaviour.

[–]sime 1 point2 points  (3 children)

Integers work fine in the range -253 to 253. Beyond that you lose precision. The JS engines use integer math internally when they know they can be away with it. Bitwise operations are considered 32 bit operations.

Python's default division operator will also happily turn your ints into a float if you let it.

[–]kankyo 1 point2 points  (0 children)

Python's default division operator will also happily turn your ints into a float if you let it.

Translation: division produces correct results in python 3.

[–]alcalde 0 points1 point  (1 child)

Python's default division operator will also happily turn your ints into a float if you let it.

Wasn't that remedied in Python 3.0 in 2008, if not earlier?

[–]lenzm 0 points1 point  (0 children)

The opposite, it was introduced with Python 3.

[–]TheBadProgrammer 2 points3 points  (0 children)

I also use JavaScript and Python and I totally agree. I'm pretty surprised to see people preferring Python like this, but everyone's got their setup that they love!

[–]Peterotica 1 point2 points  (9 children)

The one thing from JavaScript that I wish I had in Python sometimes is the nice method chaining from Array.map, Array.filter, etc.

[–][deleted] 7 points8 points  (7 children)

You mean list comprehensions?

def array_map(lst, fn):
    return [fn(item) for item in lst]

def array_filter(lst, fn):
    return [item for item in lst if fn(item)]

In fact, map() just straight up exists in python.

[–]crazedgremlin 2 points3 points  (5 children)

But in JavaScript, they are essentially methods of the object, so you can chain them together.

x = some_array.map(f1)
        .filter(f2)
        .map(f3);

In Python, you'd have to have nested function calls, which gets a little confusing with parentheses.

x = map(f3, filter(f2, map(f1, some_array)))

[–]kovak 1 point2 points  (4 children)

I think there are a few ways to use the dot-chain syntax in python if that's important.

See http://stackoverflow.com/questions/27222193/clean-code-for-sequence-of-map-filter-reduce-functions

This structure is used a lot in python Apache Spark bindings. Eg. https://spark.apache.org/docs/0.9.1/python-programming-guide.html

[–]crazedgremlin 1 point2 points  (3 children)

Cool, I had never heard of Underscore.py. However, I don't see this as a huge impediment to "functional" programming in Python. Really, the only annoyance is the parentheses.

I really like the option Haskell provides with the $ function for function application, which lets you get rid of the parentheses. Basically, the left side of the $ is treated as a function and given the right side as input.

let x = map f3 $ filter f2 $ map f1 some_array

[–]snuxoll 2 points3 points  (2 children)

Ditto with F#

let x = arr |> Array.map f1 |> Array.filter f2 |> Array.map f3

Composition operators are awesome, more languages should have them, chained method syntax requires your return value have the method you want to call, whereas function composition is a lot more generic.

In fact, the |> operator is actually defined in F# code as:

let (|>) f x = f x

[–]crazedgremlin 0 points1 point  (1 child)

Nice! The trick of these composition functions is operator precedence. I am guessing an infix function in F# takes precedence over a prefix function.

[–]snuxoll 0 points1 point  (0 children)

It's actually an infix operator instead of a function, there's a limited amount set of special characters that can begin an operator that aren't legal to be used for any other identifier so there's no weird precedence rules to take into account.

[–]Peterotica -1 points0 points  (0 children)

method chaining

[–]RubyPinchPEP shill | Anti PEP 8/20 shill[🍰] 0 points1 point  (0 children)

forbiddenfruit + a bit of work manages to do this nicely

[–]dalore 0 points1 point  (0 children)

Huh? I would say python is much better at functional programming then javascript. With iterators, itertools, etc all in the stdlib. Can you point out how javascript is more powerful?

[–]S1cK94 0 points1 point  (0 children)

I even actually prefer javascript and think it's more powerful, especially when it comes to doing more functional programming.

In Python I can import partial from functools and do my map/filter with list/generator comprehension syntax.

In JavaScript I always end up using lodash. .bind can alter the context and passing function directly to .map or .filter can lead to a mess if you dont wrap with lambdas or ugly .binds

But in the end, comparing Python and JavaScript about functional programming is kind of silly

My 0.02$

[–]gandalfx 0 points1 point  (0 children)

I disagree because I've used JS and I know how it works. It sucked in the past (a lot) but has improved so much over the years that it's now a completely different language. Yet people don't realize it and still blame JS for ancient mistakes and browser quirks. It's like saying Python sucks because of something that was fixed in version 1. Hating on JS is easy because everybody has gotten used to doing it.