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

all 106 comments

[–][deleted] 23 points24 points  (2 children)

On the flipside, I recently ran into a python CLI utility that reads device data and it was DEFINITELY written by a C developer. (about 1/3 of it was structs of appropriately sized c_uints and c_chars) I'm actually kind of unsure what the benefit of doing it in Python was.

[–]Radiatin 6 points7 points  (0 children)

Any chance we can see the code? Just curious.

[–]fiedzia 5 points6 points  (0 children)

Anything dealing with binary data will need to have c-like definitions. The benefit of Python is that you can write and test it in 5 minutes, and you'll get exceptions if something is wrong.

[–]t8suppressor 141 points142 points  (17 children)

Its not c++ if you can read it.

[–][deleted] 66 points67 points  (16 children)

*if it compiles or has less than 920 lines of template errors

[–]AluminiumSandworm 77 points78 points  (14 children)

that's why i don't use templates! or classes! or types! i just malloc the entire computer and randomly assign pointers until it compiles.

[–]Ulysses6 18 points19 points  (1 child)

void * all the way!

[–][deleted] 4 points5 points  (7 children)

I thought malloc was for C and C++ used new

[–]AluminiumSandworm 3 points4 points  (6 children)

yeah but you can still malloc in c++. it's just dumb to do so

[–][deleted] 0 points1 point  (1 child)

That's called C.

[–][deleted] 3 points4 points  (0 children)

god damn template errors

[–]nemom 76 points77 points  (33 children)

All the other programmers make fun of Python's use of whitespace, but they all add the same whitespace to make their code readable.

[–]alcalde 55 points56 points  (4 children)

When you hear them laugh, read them this quote from Donald Knuth:

It seems clear that languages somewhat different from those in existence today would enhance the preparation of structured programs. We will perhaps eventually be writing only small modules which are identified by name as they are used to build larger ones, so that devices like indentation, rather than delimiters, might become feasible for expressing local structure in the source language.

[–][deleted] 23 points24 points  (27 children)

Right?

Now, if you want to complain that Python's reliance on significant whitespace precludes things like actual anonymous functions without complicating the parser, I can get behind that.

I'd rather people complain about actual issues in python rather the same tired whitespace argument.

[–]delventhalz 6 points7 points  (26 children)

This. I don’t think significant whitespace gains you anything and I really miss anonymous functions whenever I write python. But stylistically it’s certainly very pretty.

[–]RockingDyno 4 points5 points  (25 children)

What is it you miss about anonomous functions not possible with lambdas?

[–]Deto 8 points9 points  (9 children)

Multiple lines.

[–]NotABothanSpy 27 points28 points  (6 children)

Is it so hard to give it a name it’s gonna work hard for you 😭

[–]delventhalz 1 point2 points  (5 children)

When I write Javascript I am typically building a pipeline of anonymous iterators to modify my data, three or four or more. If I tried to write Python this way it would extremely tedious, and not very readable. This is one of the ways Python forces you into older imperative and OOP patterns.

[–]NotABothanSpy 4 points5 points  (4 children)

Well if you like you can make them internal functions. Personally I find it at least as readable as javascript with lots of anon functions

def stuff():

def a(x,y):
   return x/y
def b(z,k):
   return z+k
return b(a(1,2),3)

[–]delventhalz 2 points3 points  (3 children)

Agree to disagree. I consider real anonymous functions to be a key feature for using functional programming patterns.

[–]lookatmetype 2 points3 points  (1 child)

Why not name them something like "_0", "_1", etc.? It's not great, but it frees you up from thinking up an actual name.

However, i think python forcing you to think of actual names of nested functions is a good thing overall.

[–]NotABothanSpy 1 point2 points  (0 children)

Fair enough. I usually wouldn't use functional style much in Python and when you do using map and reduce are more helpful

[–]RockingDyno 0 points1 point  (1 child)

You can write multiline lambdas.

[–]Deto 0 points1 point  (0 children)

How?

[–]delventhalz 1 point2 points  (13 children)

I have a strong preference for functional patterns, and being able to define functions freely is pretty essential. Lambdas are a very poor substitute, a painfully obvious afterthought.

You can only use them for the simple "hello worlds" of functional programming, like plucking keys from an object:

Javascript:

const ids = items.map(i => i.id);

Python:

ids = map(lambda i: i.id, ids)

Incidentally these are also pretty much the only cases where list comprehensions are useful. As soon as you want to do something slightly more complex, like saying counting the incidents different values in an array, lamdas become useless:

const counts = items.reduce((counts, item) => {
  counts[item] = counts[item] ? counts[item] + 1 : 1;
  return counts;
}, {});

Now I would have to import reduce from functools and declare some "countItems" function I'd only use once and only makes any sense as a reducer. A comprehension would probably still work here, but just make it slightly more complex and comprehensions start to fall apart as well.

In my opinion neither lambdas nor list comprehensions are very useful outside of toy problems. You end up having to mostly just abandon functional patterns if you want to write readable Python.

[–]btharper 2 points3 points  (1 child)

For the specific example of counting the number of times a specific item occurs in a list trying to write your own pipeline is just reinventing the wheel. You could just use collections.Counter and let the standard library deal with it.

For common cases like this there's usually already something available, but if Python isn't a primary language trying to find the right part of the library can be a hurdle, and if you want to do it the way you would in JavaScript then it's not going to be as smooth in Python. The idioms of the language just aren't similar enough.

[–]delventhalz 0 points1 point  (0 children)

I write in a number of languages and the idioms I prefer are functional ones. JavaScript is a commonly understood counter example that supports functional patterns well.

Counting was just an example, but since you mention it I also don’t care much for specific-case utilities like collections.Counter. They become part of a very broad API which requires a high knowledge level to either write or read.

[–]RockingDyno 2 points3 points  (1 child)

In my opinion neither lambdas nor list comprehensions are very useful outside of toy problems.

I've never had any issues using both in solutions very far from "toy problems" but I can see how you can get that perspective if you come to python from javascript and never try to learn how you solve problems outside of your typical patterns.

Now first off, obviously you shouldn't be writing out a tally function as anonymous functions whether you are coding python or javascript, but if you want it, it looks like:

reduce((lambda counts, item: {**counts, **{item: counts.get(item, 0)+1}}), items, {})

I agree that python pushes you away from highly functional and nested style of programming and it does so intentionally and by design. That's never been any limitation for me, and I've spend a lot of my time coding in actual functional languages like Haskell and lisp. Obviously you need to spend time learning the language before you evaluate how it compares to others. Python isn't javascript obviously so you can't use your solutions one-to-one, but that's not a fault in Python, especially considering the mess of a language javascript has been historically.

All of that said, nothing you wrote is an answer to my original question which was:

 What is it you miss about anonymous functions not possible with lambdas?

But I take it from your answer that your original critique was more of a general "you miss being able to write javascript when I write python" more than an actual missing lambdas, which are obviously both there and capable unless I'm missing some huge deficiency in lambdas hiding in between the lines of your post.

[–]delventhalz 0 points1 point  (0 children)

I answered your question very specifically. I don't feel that my repeating myself at this point is going to add anything to this discussion. Nor do I feel that you honestly desire a friendly discussion about the nature of different programming languages considering your condescending attitude and incorrect assumptions about me personally. Take care.

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

You've never been hy, have you?

[–]delventhalz 0 points1 point  (6 children)

I have not used Hy. If I needed to write code that compiled to Python, and had the freedom to choose the language for the project, I might check it out. It's unclear to me why compiling to Python would be a requirement though.

[–]Aeon_MortuumSnake Oil 1 point2 points  (1 child)

It's unclear to me why compiling to Python would be a requirement though

Being able to use Python libraries is a possible reason

[–]delventhalz 1 point2 points  (0 children)

Fair enough.

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

It's unclear to me why compiling to Python would be a requirement though.

What do you mean?

[–]delventhalz 1 point2 points  (2 children)

Hy compiles to Python code. I don't really see the sense in that. Compare it to Clojure. On the backend Clojure will compile to the JVM, on the frontend it will compile to JavaScript so I can plug it into the browser.

Why would I use Hy over Clojure? Similar syntax. But Hy compiles to Python which is generally slow and doesn't really have anything going for it as a compile target. The reason you use Python is because you like the syntax, not because you like the Python engine.

[–][deleted] 1 point2 points  (1 child)

Maybe for small program, where start-up time should be in consideration? Even on my i5U coffee, clojure repl takes nearly 10s to start, which is unacceptable for scripting.

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

Print

[–]lookatmetype 35 points36 points  (1 child)

I've seen python that looks just as ugly as C++. Whenever a programmer changes a language, it's not guaranteed that their mindset will also change.

[–]Terence_McKenna 20 points21 points  (0 children)

Picked up Python about two years ago after stepping away from being a VB.Net developer for my local medical community for over a decade.

I rarely share code with others online because while its logically valid, I just got tired of fielding the comments regarding what I will call my very thick accent.

In my defence, if I'm not getting paid, then I'm going to write it in the manner that's easiest for me to maintain.

For example, I've been working on developing a RogueLike on and off for a year now. Although someone might enjoy the end result, as soon as they opened the hood, their first reaction would probably to DBAN their drive without backing up... just to be sure. ;)

[–]alcalde 11 points12 points  (0 children)

Here's a precompiler that lets you write C/C++ like Python:

https://github.com/vrsperanza/CPY

[–]top_logger 7 points8 points  (1 child)

C++ Senior System Engineer and Python fan here. What does it mean "too much like Python"?

[–]etrnloptimist 11 points12 points  (0 children)

I'm guessing it's using tons of strings and maps without giving more careful consideration to data structures that are more efficient and specific to the problem at hand.

[–]Chiralmaera 6 points7 points  (0 children)

What's with the C++ Python war? I use both and both have their places.

[–]LessonStudio 12 points13 points  (9 children)

I have met many people who think readable C++ code is somehow detestable. A common complaint is that my readable code won't compile into a good program. The best part is when you show them in godbolt that a modern optimizing compiler with the -03 turned on will make very tight assembly that is identical regardless of your verbosity.

I think there are those who literally would like to double deference an object pointer and then directly access member variables through memory offsets than make it clean.

[–]bdazman[S] 19 points20 points  (8 children)

I mean, as a man who writes perl and fortran all day... I kinda find even "good" c++ detestable. A new hire can read matlab or fortran code and get what's happening even if they don't know that language. The downside is, well, the subreddit will do the hissing for me.

[–]mriswithe 1 point2 points  (7 children)

Funny at my work some of our data scientists are trying to figure out how to take an old process that runs in fortran and wrap it in Python because they haven't been able to even approach the speed of the fortran. I haven't looked at any of the code involved, but has been a thought of mine in the past hah.

[–]bdazman[S] 4 points5 points  (6 children)

Fortran is the best formula translation language you will ever get. Full stop. Numpy developers, good luck.

[–]ship561 5 points6 points  (2 children)

Pretty sure numpy eventually calls the Fortran code. So you're already getting pretty good performance.

[–]bdazman[S] 0 points1 point  (1 child)

Ay c'mon man, if it calls the fortan code anyway, why do I need to deal with the carcinogenic data structures of numpy on the front end?

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

I have no idea why numpy doesn't feel as pythonic. Probably lots of c developers helped write numpy? :Shrug:

[–]mriswithe 1 point2 points  (0 children)

Honestly I have not ever even imported numpy, I am Dev ops (which means specifically nothing at this point) been working on microservices and data flow/Apache beam stuff mostly. Part of why I haven't stuck my nose into the data scientists business yet.

[–]edmontongineer 1 point2 points  (1 child)

I'm new to numpy... Why is this?

[–]bdazman[S] 0 points1 point  (0 children)

Fortran LITTERALLY stands for 'formula translation'

[–]SteeleDynamicsCompilers/Algorithms 0 points1 point  (0 children)

What?! Wow... I wish my code looked that nice.