r/Python • • Jan 24 '15

Why a C++ programmer might say "I could never write Python"

http://jvns.ca/blog/2015/01/24/why-a-c-plus-plus-programmer-might-say-i-could-never-write-python/
74 Upvotes

141 comments sorted by

99

u/Literally_a_Car Jan 25 '15

I once heard a C++ programmer say "oh no, Python, that’s so hard, I could never write that. I’m so used to having a compiler!"

[skepticism]

13

u/fivetoedslothbear Jan 25 '15

And what's so wrong with using a lint-like tool? I have flake8 installed in my Vim editor and it says:

elephant.py|2 col 1| F821 undefined name 'elephnt'

Checkers like this are easily integrated into editor save hooks and unit tests. They catch a lot of things that compilers also catch, especially the use of undefined variables, and variables that are set but never used.

5

u/jaseemabid Jan 25 '15

@fivetoedslothbear, there is only so much linters can do. Flake8 won't help you when you write user.helo() instead of user.hello(). That is where compiled languages shine.

1

u/shinigami2057 Jan 25 '15

Static checkers integrated into your IDE are incredibly helpful. I can't go back to the days of editing code in raw emacs/notepad++; PyCharm's integrated static checker is great.

2

u/grotgrot Jan 25 '15

I use emacs which has flycheck that uses flake8 for Python code. A boatload of languages are supported.

9

u/[deleted] Jan 25 '15

Well, as someone who works primarily in both C++ and Python, I can see why working primarily with an interpreter rather than a compiler can be confusing. It's difficult to test certain aspects of code in Python that would otherwise already have been caught in a compiled language, which is the author's point.

6

u/Literally_a_Car Jan 25 '15

I don't really have an issue with the author's point overall, but... c'mon, let's be real here. Python is one of the least intimidating languages in existence. It's one of the draws of the language, and it's great. For a C++ programmer, this should go doubly. I'm not saying Python is without its own set of pitfalls and trappings, like any language, but when you're coming from C++ there should be absolutely nothing threatening whatsoever about Python.

"oh no, Python, that’s so hard, I could never write that"? If this truly was uttered by a C++ programmer, I wouldn't know what to say, besides maybe "... wait, are you serious?" I suspect that programmer might likely have some other issues. I would be curious as to how they managed to come into such a mindset, though.

3

u/[deleted] Jan 25 '15

I completely agree. I think C++ is a harder language, overall. But I also do agree with the author's point that Python has a "hard" aspect to it which is not experienced in C++. I guess the author would have done better to say "given people with experience in both languages, this is what a C++ programmer would find harder to achieve in Python"--which wouldn't be an interesting article, since basically it's been rehashed hundreds of times in various articles and arguments. So, I guess the article isn't actually that interesting.

7

u/[deleted] Jan 25 '15

This has been my experience as well. In order to get around it, or to still be able to quickly write python, I use (the) IPython (notebook) where expressions can be immediately within a running kernel (interpreter). This has saved me loads of time & I can't imagine writing python code the old way...

6

u/mtelesha Jan 25 '15

Also iPython is being renamed Juypter and works with Julia, Go, Haskel, R, Ruby and many others. iPython is the bits that are Python based and the rest is Juypter.

It is the Best thing since ever.

-1

u/alcalde Jan 25 '15

So to get around not having a compiler, you're saved by... an interpreter. Sorry, I don't follow how this supports the person you're replying to. It seems to be doing the opposite.

2

u/[deleted] Jan 25 '15

It's interactive programming, like lisp/emacs with slime.

1

u/stormcrowsx Jan 25 '15

Considering the interpreter does a compilation as its interpreting its not a whole lot different.

1

u/[deleted] Jan 25 '15

One notable difference is that for a statically compiled language, that compilation and resultant error checking occur at compile time. For an interpreted language, they occur at run time. If an error exists in an infrequently taken code path (e.g. an error handling section), it is possible that it won't be caught with limited testing, whereas it would have been caught at compile time in a static language. This increases the importance of extensive testing (or lint type tools).

2

u/alcalde Jan 25 '15

What specifically can't you easily test in Python that would be caught by a c++ compiler? If you're doing TDD it shouldn't make any difference, should it?

5

u/d4rch0n Pythonistamancer Jan 25 '15 edited Jan 25 '15

Any type-checking.

I have a function which returns an integer. Somewhere along the line I assigned a string to it.

Python goes "OKAY! It's perfectly fine that I just set the variable x = 5 and then later set x = "foo". It doesn't matter that def prime_number() is returning a string."

C++ compiler goes "Wtf you explicitly stated you want prime_number() to return an integer and you're setting one of its returns to a string. I'm not even building this."

Another thing might be scope. Write a function and put print( this_var_does_not_exist_yet) as its block. It will gladly run until it hits that statement where it tries to print a variable that was never instantiated. The C++ compiler knows each and every variable that exists on every line, and it'll know that that variable was never declared, so you can't be using it. It won't compile.

However, the common Python argument is that the types of bugs the compiler catches aren't the hard ones to find and fix, and I tend to agree. They're the minor sort of bug in your debug cycle.

But I have run into bugs that would have been caught, that were stupid mistakes. In an argparse argument parser, instead of setting my help string for an argument to "does so and so", I accidentally declared "does so and so" as the default value of that flag. Later, I keep checking the truthiness of that flag... and it was always true, no matter whether I set it or not with --flag on the command line. I couldn't figure it out. Then reading through slowly, I realized that it was always evaluating to true, because a non-empty string is true.

Dumb stuff that the compiler would have saved me a couple of hours, but having more sleep and paying better attention would have saved me a couple of hours as well.

I don't see why TDD would help you get around these specific issues though. You're writing the tests and it's your responsibility to cover every edge-case, but even someone who does TDD won't check the type of every variable at every step along the way. Usually, you're only checking that the return values are good, not that something which you thought was a boolean True turned out to be your help string. But I do agree TDD will help you get around certain unpredictable aspects of dynamic typing, simply because you're testing every part of your code every time you run the test, and if the return value and type matches, you probably got types right, or they are as you expect. But again, if you assert some_func() > 10, and some_func() unexpectedly returns the string "5", that test will pass.

3

u/[deleted] Jan 25 '15

I would say what I said applies to working on larger projects, where TDD can be trickier and results can be confused by the dynamic typing process. This can, as with all things in the programming world, be worked around, but I would say that C++ static typing saved me from a lot of potential mistakes, especially when I'm working with large OOP projects. This isn't to say that it's impossible in Python, obviously, but I think it's more tedious.

1

u/shinigami2057 Jan 25 '15

TDD helps, to an extent. It helps to run and keep running all the code in your project that is testable, and will catch lots of errors that a C++ compiler would catch. But where it might fail you is the 'glue' code that isn't always test-driven. You need acceptance tests for that.

The primary issue is that in Python you have to run ALL of your code to really trust its reliability. I love Python, don't get me wrong, but you really need all the help you can get in this area (TDD, acceptance testing, pylint, etc.). I've written tons of Python programs that run for weeks at a time, and it's insanely frustrating to get 50+ hours in and have to start over because of a typo in an exception handling routine. That just doesn't happen in C++; you get different classes of errors to ruin your day.

8

u/alcalde Jan 25 '15

I've had a lot of conversations with Delphi developers, including some who have said they couldn't imagine ever writing code without an IDE. And it's the same thing with static typing - they spit out the aphorism about bugs being caught early and a great deal of imagination about unspecified bugs that would cripple the code. And this is only if they can get past the significant indentation. Something along the lines of "Layout should never have syntactical meaning to the compiler" was given as a justification for the oppositions, which got several "+1"s and "I agree!"s until I pointed out that that was merely begging the question.

There's a lot of weird psychological objections to Python that don't seem to be grounded in reality. I believe it was Saul Alinsky who wrote that there are two reasons people do anything: the reason they give and the real reason. I imagine so much of the objections to Python boil down to it just being/looking different than whatever someone is used to.

Oh, to top it off: one of Embarcadero's "Evangelists" wrote a blog entry during the period that it looked like South Africa was going to mandate the use of Delphi for high school programming students (quashed by popular uprising among educators). Hold on to your hats for this one:

Delphi really helps this in a number of ways; including having a friendly easy to read language, full object orientated notations, but most importantly by having a compiler! The benefit of having a compiler is key in encouraging students to take up and stick with programming. Without a compiler it is hard for students to understand where errors are being made. Sounds a small thing, but makes a massive difference and has a huge impact on writing code where you can quickly see where there are errors and help address them. If you compare that to Python or Java Script, your soon understand what a benefit a proper compiler brings.

As the icing on the cake, he didn't approve of any posts that were critical of his article. The only approved post told him how great it was.

So no, I'm not skeptical at all.

3

u/mrcodewizard Jan 25 '15 edited Jan 25 '15

I completely understand the IDE thinking of Delphi devs. Even though I write a lot in a text editor, by necessity; I do miss using an IDE. Even after several years of working in a text editor, I find myself missing tools I had in the IDEs I used.

Delphi will spoil you.

I wish Mac had decent programming tools.

0

u/justphysics Jan 25 '15 edited Jan 25 '15

I wish Mac had decent programming tools.

Wat?

These aren't all IDE's but there's: Emacs, Vim, Sublime Text, Peppermint, Eclipse, WebStorm, Komodo, Chocolat, etc...

39

u/[deleted] Jan 25 '15 edited Jan 25 '15

I like Python a lot, but the benefits of static typing are undeniable. Whenever I write Ruby or Python after writing a ton of Haskell I feel half of the tests I write are a job for a computer, not for a human. You do need tests in Haskell, but at the integration level, not at the unit level.

Basic correctness of my function is guaranteed by the compiler, not by myself writing a ton of repetitive tests. The feedback is much faster as well. I type :wq and GHC is there within half a second telling me where I fucked up. This check the whole program as well. Running the test suite for a similar program in Python would take at least a few minutes.

-4

u/alcalde Jan 25 '15

I like Python a lot, but the benefits of static typing are undeniable.

Robert Martin denied them.

http://www.artima.com/weblogs/viewpost.jsp?thread=4639

We've also had the results of experiments published that showed development time with dynamic languages was quicker than with static typing but with no increase in bugs:

http://neverworkintheory.org/2012/10/25/an-experiment-about-static-and-dynamic-type-systems.html

So, not only is it not undeniable, it's quite easily deniable. This and other empirical evidence supports the superiority of dynamic typing.

Whenever I write Ruby or Python after writing a ton of Haskell I feel half of the tests I write are a job for a computer, not for a human.

You'd rather write a lot more code?

You do need tests in Haskell, but at the integration level, not at the unit level.

Aren't functional languages' type systems just coded tests in disguise?

Statically typed languages are filled with complexities that simply don't need to exist in dynamically typed languages.

20

u/TIGGER_WARNING Jan 25 '15

So, not only is it not undeniable, it's quite easily deniable. This and other empirical evidence supports the superiority of dynamic typing.

Why does every programming argument devolve into this Superman vs. Batman level of nuance?

8

u/tilkau Jan 25 '15

Politics.

Arguments are soldiers. Once you know which side you're on, you must support all arguments of that side, and attack all arguments that appear to favor the enemy side; otherwise it's like stabbing your soldiers in the back—providing aid and comfort to the enemy.

Unfortunately, the more trivial the question, the worse this behaviour seems to get. If there is any pointless distinction to be made, people will make it.

4

u/TIGGER_WARNING Jan 25 '15

It was a rhetorical question, really. It's just obnoxious how this is such a marginal problem in every other area of STEM discussion, but a constant and derailing problem in programming talk.

It's like programmers and only programmers get some specific mutation for low-empathy, high-ego, pseudo-technical yapping over the most trivial bullshit.

Hell, couple of hours ago in /r/programming this nonsense happened. Just a daily stream of sophistical garbage to try to filter out or let your eyes glaze over.

Maybe it's an epigenetic effect... all the years of cave-dwelling or something.

Either way, it's a running reminder that most programmers base their professional views primarily on unscientific/dogmatic buffoonery.

And that I can't stand easily 95% of them despite getting along well with most other technical specialists.

3

u/Pas__ Jan 25 '15

Well, you know. It's a hard question, we don't really have enough good quality data, we lack good set of criteraia to evaluate what we have, and to design experiments to resolve the question. But since it's important (and takes up a large chunk of programmers' lives, ...) we like to talk about it.

3

u/TIGGER_WARNING Jan 25 '15 edited Apr 26 '15

Wasn't my intention to complain about the topic of debate itself — just the arbitrary pressure to end it, like so many programming debates, with black and white conclusions.

Scientists say what you just did all the time. Sure, their debates get very heated at times, but they rarely end in this neener-neener level of splitting that constantly ruins programming debates and leaves anyone not already radicalized in one direction or the other unable or uninterested in participating.

2

u/shinigami2057 Jan 25 '15

Totally agree. I write production code in both C++ and Python on a daily basis, and it's really important to understand the advantages and limitations of both. Static vs. dynamic typing is a huge part of your design and test and needs to be well understood for programmers on both sides of the field. But I get so frustrated when it turns into a black-and-white 'my language design is better than your language design, I don't know how you get work done!' bitchfest.

1

u/TIGGER_WARNING Jan 25 '15

but... dynamic typing is superior. dontchu kno that?

#confused

1

u/alcalde Jan 26 '15

It is. It's an empirically proven fact. You're trying to insist that this is a qualitative issue for which there is no right or wrong answer. That's simply not true. We can measure effects on productivity and errors. Heck, the person who performed the experiment I linked to created an entirely new language for the test - two versions actually that differed only in typing. Competent programmers were given a tutorial in each - longer for the static variant. The programmers who undertook exercises in the dynamic version took less time to code the problems and had the same level of accuracy and no more bugs statistically than the static group. This researcher has followed up with other, related, experiments to test static vs. dynamic including its worth in APIs (which was indeterminate).

These things are simply claims about reality, and claims about reality can be measured and tested.

1

u/Pas__ Jan 25 '15

ruins programming debates and leaves anyone not already radicalized in one direction or the other unable or uninterested in participating

Ah, totally agree. Though, I find that this only happens online, where people don't know eachother and just reply to the given ~10 sentence blurb, or at best to the whole "thread".

And, above a level it comes down to preference. I know people who can write really shitty C++ and still get away with it because it gets the job done, and others who write wonderous Python which just remains unfinished.

0

u/alcalde Jan 26 '15

No nuance here, just the "science" part of "computer science". We have peer-reviewed literature that has found that the "conventional wisdom" that static typing provides benefits isn't confirmed in reality. There are several more papers I could have posted.

There's nothing wrong with pointing out facts. To this day it's accepted wisdom that object-oriented programming speeds software development, but as one computer scientist has said, every time that claim is made computer scientists have to stand up and say that it has yet to be proven. In fact, there's actually almost nothing in the literature about the subject. The very few papers that exist on OO benefits did not find productivity benefits at the small or medium level.

3

u/TIGGER_WARNING Jan 26 '15 edited Apr 26 '15

Ah, we're being scientific. Then surely you must've glossed over the actual conclusions of that string of papers on static vs. dynamic typing by the same author you linked (Hanenberg) — particularly the ones that tip in favor of static typing — to arrive at the conclusion you gave.

Since you were being scientific, and certainly weren't trying to use "science" (i.e. the results of a single paper) as a bludgeon, I'm sure you'll go back and amend your statements as to the categorical superiority of dynamic typing.

Now that I've done some better review of the literature — which is a total minefield of shoddy work that wouldn't fly anywhere in cogsci proper, by the way — I think I would like for you to link those several other papers. It'll give me a clearer idea of whether you're completely full of it or just have a naive/rudimentary understanding of how behavioral science works. The latter can be cleared up.

And from your other reply:

You're trying to insist that this is a qualitative issue for which there is no right or wrong answer. That's simply not true.

Quantification of performance doesn't automagically nullify qualitative considerations in any domain. A new algorithm might dramatically outperform one you currently implement, but not fit well into the larger architecture you're stuck maintaining, for instance. Pick any field and you should be able to think up 10 solid examples in as many minutes.

Which is why it's so bullheaded to say stuff like "dynamic typing is superior" and call it a wrap. Even if that were an empirically uncontroversial fact (and it isn't currently), there are many, many factors at play in picking a language / language feature set for a project that lie well beyond elementary questions of performance.

0

u/alcalde Jan 26 '15

Off the top of my head, there's a good overview of several papers at

http://danluu.com/empirical-pl/

Quantification of performance doesn't automagically nullify qualitative considerations in any domain.

It's always possible to invent an ad hoc reason why something might not work in a particular situation, but that doesn't nullify the general conclusion. Mike Tyson might lose a fight to Johnny Weir if Tyson was seriously ill and Weir was hopped on stimulants and had been secretly training for three years, but that doesn't mean we can't say "Mike Tyson would kill Johnny Weir in a street fight".

Which is why it's so bullheaded to say stuff like "dynamic typing is superior" and call it a wrap.

I'm not the one who was bullheaded. The post I originally replied to claimed that the benefits of static typing were "undeniable". I countered with the fact that there's not a lot of empirical research on the subject, but of the papers that exist and generally past muster there are several that either show advantages for dynamic typing or no advantage for static typing. In this case, if we're going to be making any pronouncements, it's that dynamic typing has demonstrated some benefits (not huge benefits) while static typing hasn't demonstrated what its proponents tend to claim.

Why did the original post get a pass?

Even if that were an empirically uncontroversial fact (and it isn't currently), there are many, many factors at play in picking a language / language feature set for a project that lie well beyond elementary questions of performance.

No argument there. But people shouldn't expect to come into a python subreddit and declare by fiat the wonderfulness of static typing and expect to remain unchallenged.

1

u/JavaDroid Jan 25 '15

Well, first, that Robert Martins post wasn't what you chalked it up to be.

Second, rudimentary, solitary assignments that generally favour dynamic typing are hardly empirical evidence for one being better than the other.

2

u/alcalde Jan 26 '15

Martin:

I thought an experiment was in order. So I tried writing some applications in Python, and then Ruby.... I was not entirely surprised when I found that type issues simply never arose. My unit tests kept my code on the straight and narrow. I simply didn't need the static type checking that I had depended upon for so many years. I also realized that the flexibility of dynamically typed languages makes writing code significantly easier. Modules are easier to write, and easier to change. There are no build time issues at all. Life in a dynamically typed world is fundamentally simpler.

So he a) didn't need static typing and b) gained benefits from dynamic typing. How does that not equate to Martin denying the benefits of static typing, which is what I asserted?

Second, rudimentary, solitary assignments that generally favour dynamic typing are hardly empirical evidence for one being better than the other.

A peer-reviewed article by a computer scientist that gets published in a computer science journal isn't empirical evidence? There are (a few) more where that one came from, but it was the most ambitious to date and the one that answered the most objections leveled at previous experiments.

2

u/JavaDroid Jan 27 '15 edited Jan 27 '15

OK. Well, we just fixed an error in our program that managed to skid by and cost an estimated 7,000,000 over 20 years that was the result of dynamic typing. It was only found by accident. If it were static typing, the error would have never existed in the first place.

That must be evidence that dynamic typing stinks.

It is perfectly fine that you prefer dynamic typing. But let's not pretend it isn't without its pitfalls. It is slower by nature, plus you can get strange bugs that other typing will catch at compile time. In dynamic typing, you also run the risk of a minor update having a slight change in behavior that either breaks your code or forces a rewrite.

-1

u/mrcodewizard Jan 25 '15

We've also had the results of experiments published that showed development time with dynamic languages was quicker than with static typing but with no increase in bugs ...

What a crock.

Real life clearly shows the opposite. As an example: SublimeText cough vs TextMate, BBEdit, hell even VIM is better than ST. ST is the poster child for not using a scripting language.

1

u/Lucretiel Jan 25 '15

I feel like you're using a different Sublime Text than I am.

1

u/Pas__ Jan 25 '15

Real life is large and diverse.

I guess in simple web development typing is largely irrelevant. If it works, good. If it doesn't ... lemme fixit realquik, thxbai.

In military software, you can't playing with rockets alone, please always invite Hindely and Milner too.

1

u/xXxDeAThANgEL99xXx Jan 25 '15

You do need tests in Haskell, but at the integration level, not at the unit level.

If your goal is to catch typos, integration tests do that pretty much.

I actually became much happier as a programmer when I realized that 99% of the hype about unit testing comes from the fact that having any kind of automated tests is awesome, from the people who just discovered that or the TDD that forces them to write tests, any tests.

In my experience, trying to write unit tests for everything sucks hard because they make your code inflexible (oh the irony). It's much easier to refactor a bunch of code, changing argument types and return values, and to make yourself to realize that you should do that, when there isn't 5x the amount of code in tests that you should throw away and rewrite from scratch.

In my opinion tests should test conformance at relatively stable API boundaries, precisely because they are relatively stable.

Unit tests are for a function that implements some complicated generic algorithm. Since it's generic, the API would not change or would change in a trivially accommodated fashion.

Doctests are smoke tests and are also at the API boundary (you're writing documentation for that function for someone, aren't you?).

Any kind of larger scale tests is where it is, you verify that the code does the needful (he he). You can make them as detailed as you want, like, the code should do the right thing with this data and that data, if you can't exercise some part of your code that you're concerned about from that level then it's probably dead code, no?

Like, a single actually sane reason for preferring unit tests in particular is that when they fail, they fail much nearer to the actual bug. Oh well, I'm sure that I'll be able to find the bug much faster using a debugger than it would take to write all those tests.

Also, yeah, if you're concerned about testing failure modes, C++ leaves you as much in the dark as Python in this regard, I don't know about Haskell.

-18

u/bluecamel17 Jan 25 '15

Please stop comparing Ruby to Python. They are VERY different.

3

u/[deleted] Jan 25 '15 edited Jan 25 '15

I think you misunderstood the structure of the argument. jsantos17 has not compared Ruby and Python. The argument was "Ruby after Haskell or Python after Haskell", with broader context being "statically typed language after a dynamically typed language".

Edit: jsantos17, not bluecamel17 :)

1

u/bluecamel17 Jan 25 '15

I AM bluecamel17, and I get your point, but Python and Ruby handle types differently and no one here seems to understand. Meh...

Edit: to be clear to your point, static typing is possible in Python. Not so much with Ruby.

-3

u/bluecamel17 Jan 25 '15

Sorry I annoyed the Ruby folks, but it's true and not an accurate comparison. You might as well compare Perl and Python in the same way.

10

u/SilentTemple Jan 25 '15

And it wouldn't be a bad comparison either.

-3

u/bluecamel17 Jan 25 '15

Hey, how about one of you joins the conversation instead of the lazy downvotes?

-5

u/bluecamel17 Jan 25 '15

Yes it would. Perl was inspired by LISP, Python by C. Even Larry Wall will tell you that he borrowed the object model for Perl 5 from Python and then changed his mind later. They are only similar in that they are modern programming languages. They are based on very different ideologies.

2

u/an_actual_human Jan 25 '15

Python was inspired by LISP as well (that's where functional elements such as reduce and map came from). Every modern language is inspired by a lot of prior art.

Anyway, Python and Ruby (the languages proper) seem very similar in most ways. If you can think of a language that is different from one (e.g. Python is very different from C or Haskell) you'll see the other one is also different from it. Small distinctions in features are a matter of taste. Distinctions in philosophy even more so.

0

u/bluecamel17 Jan 25 '15

Inspiring a couple of features is very different, but I give up. Ruby == Python.

0

u/an_actual_human Jan 26 '15

It has inspired the same features in Ruby. If anything Python is closer to LISP than Ruby is (functions are first class citizens).

0

u/[deleted] Jan 25 '15

[deleted]

-1

u/bluecamel17 Jan 25 '15

From a thousand yards away, maybe. I'm not knocking on Ruby. I love Ruby. Not as much as I love Python. But let's not be stupid and compare them as equals just because they're both dynamic languages that can do things.

8

u/crowseldon Jan 25 '15

As someone who writes both python and c++ at work I'd say that unit/integration tests (and a fair share of tdd) would solve most problems.

there's a myriad of ways in which you can make silly mistakes. heck, even with super IDEs you can end up refactoring wrongly and screwing up unwillingly a a framework header.

7

u/bheklilr Jan 25 '15

And maybe this is part of why Haskell programmers get so attached to Haskell – because they’ve invested so much in learning the type system, and those skills don’t transfer well to other languages like Python.

I find this interesting as someone who chooses Haskell at home but uses Python professionally. The skills I've picked up with Haskell influence how I write Python code, and I like to apply lessons learned in Python to how I write Haskell. Just because they're nearly at opposite ends of the spectrum doesn't mean that they don't have commonalities. Python has a lot of functional tools available, the core built-in data types are surprisingly similar to those in Haskell; tuples, lists, sets, lazy evaluation (iterators/generators in Python), and comprehensions.

1

u/[deleted] Jan 25 '15

Python's lambda syntax is really horrible compared to haskell's though :(

3

u/alcalde Jan 25 '15

lambda (input variable): (return value)

why do you feel that's horrible? What would you rather see?

4

u/codygman Jan 25 '15 edited Jan 25 '15
\(input variable) -> (return value)

That is Haskell's lambda syntax. It can also be multiline. I'm guessing this is what GP was talking about.

EDIT: Thanks autocomplete...

3

u/[deleted] Jan 25 '15

Hm, am I missing something here?

What makes a

lambda (input variable): (return value)

to this almost identical Haskell equivalent

\(input variable) -> (return value)

?

I never really used lambda function before I got started with pandas. Now, in combination with the apply method, I really love them and they finally make sense to me aesthetically!

1

u/codygman Jan 25 '15

I'm not sure what you are asking, but I'm going to guess you are asking "What makes the Haskell version cleaner than the Python version?". Correct me if I'm wrong.

If you are asking though, a simple code example will explain the best.

filter(lambda x: x == "pickme", ["a","b","pickme"])

filter(\x -> x == "pickme", ["a","b","pickme"])

I can't think of a good practical example of nesting lambdas at the moment, but thats were the more minimal Haskell version of lambdas really shines:

map((\x -> map (\y -> y * 5 - 3)), [1,2,3])

Versus:

map((lambda x: map (lambda y: y * 5 - 3)), [1,2,3])

2

u/[deleted] Jan 25 '15

Thanks for the nice examples. However, both Python and Haskell look conceptually pretty similar to me with respect to the lambda function. I can't see much difference

1

u/codygman Jan 25 '15

Well that wasn't idiomatic Haskell, I was just showing differences in lambda style. I'll post examples when I'm back at a computer.

1

u/codygman Jan 26 '15

Python version using lambdas and map (slightly different from above):

map((lambda x: x * 5 - 3), [1,2,3])

Maybe more idiomatic Haskell version taking advantage of partially applied functions instead of explicit lambdas:

map ((* 5) . (- 3)) [1..3]

Actually the above doesn't work because of syntax weirdness with - and I ended up having to use:

map ((*5) . (subtract 3)) [1..3]

Meaning I'd just probably have used an explicit lambda like below :)

map (\x -> x * 5 - 3) [1..3]

I try to use these patterns in Python where applicable. It might be interesting to check out:

https://pypi.python.org/pypi/PyMonad/

1

u/[deleted] Jan 26 '15

https://pypi.python.org/pypi/PyMonad/

Thanks for the heads-up. I kind of like those little idiomatic things!

1

u/Steve_the_Scout Jan 25 '15

Meanwhile, with C++:

[=|&|(=|&)var-name] (parameter-list) (mutable) (-> return-type) { body }

Of course a few of those are optional, e.g. you can do

[]() {
    return "Hello, world!";
}

but it's still pretty ugly (I say this with C++ as my favorite language). I wish it used Python's lambda syntax, with slight modifications to make it compatible with C++ of course.

6

u/[deleted] Jan 25 '15

C++ has been my favorite language for decades. Python is delightful to use. It was easy as hell to slip into Python. Some of the peculiarities were fun to learn (being able to swap out underlying functionality without building a full blown mocking scaffold was an eye opener.) But it wasn't "difficult", CERTAINLY not as programming languages go.

15

u/[deleted] Jan 25 '15 edited Jan 25 '15

I like this post and agree with the main point: Writing safe & efficient code in dynamic languages is not so trivial. However, therefore you have other problems like memory leaks etc. in C / C++.

Since basic coding in Python is super simple, learning how to write safe & efficient code is just something that has to be additionally learned (as follow-up to Python intro tutorials). In contrast, this skill is likely be learned by a C / C++ programmer on the fly as consequence of the language design. But that doesn't mean Python is more difficult than C++ or the other way around: One just has to invest about the same time to learn Python thoroughly that he or she would invest for learning C++.

Like I said, I like this short comment, but the "typo example" is not a good one... It will raise a syntax error upon execution, so what's the problem? Also, things like that wouldn't happen if one uses tab-complete...

13

u/[deleted] Jan 25 '15

I thought the example was ridiculous. Has the author never heard of an IDE?

8

u/[deleted] Jan 25 '15

I would say Python is much easier than C++, even given everything you said. That's not necessarily a bad thing.

4

u/le8ip9pu Jan 25 '15

It will raise a syntax error upon execution, so what's the problem?

This is the problem. You have to execute the code to be sure that it is correct on most basic level.

If you don't execute it nor lint it, you have practically a Schrödinger's code - it may work or it may crash miserably. Catching of trivial errors ASAP (on compilation stage) is invaluable.

Regarding the bit about "tab-completion": It is like an artificial leg. You can move when you have it but what happens if you lose it?

5

u/[deleted] Jan 25 '15

This is the problem. You have to execute the code to be sure that it is correct on most basic level.

That's where doctests or unit tests come in handy -- a good habit anyway.

6

u/le8ip9pu Jan 25 '15
This is the problem. You have to execute the code to be sure that it is correct on most basic level.

That's where doctests or unit tests come in handy -- a good habit anyway.

Sure, still you have to write it, to have the same amount of certainty which you have "for free" in case of "compiled languages".

I have found myself that it is pylint which acts as a partial substitute for compilation stage verification.

If you have to provide extensive set of regression tests to achieve the acceptable level of program correctness, then the development is not "fast" and "efficient" anymore. Especially, that the things you would like to be especially sure about are the things which aren't easy to test and/or reproduce (e.g. IO errors on different stages of stream processing). Yeah, you have stubs, but it is even more work to do to achieve what you would like to catch on compilation stage.

Still, IMO, pylint (or its equivalent) is a must have for almost everyone developing in Python.

4

u/alcalde Jan 25 '15

This is the problem. You have to execute the code to be sure that it is correct on most basic level.

Sorry, but you have to execute the code to be sure it's correct on the most basic level anyway.

Function (a, b : Integer) : Real;

can be used by the compiler to catch "possum" being passed into the function, but it won't catch that b is supposed to be greater than a or that the return value is supposed to be positive.

If you don't execute it nor lint it, you have practically a Schrödinger's code - it may work or it may crash miserably.

Again - for what piece of software outside of a 2-line code example would this not be true for, regardless of language or type system?

You're making me think back to when I was a teenager with a summer job at a local company in the 1980s. I was working on coding a front end for a piece of software someone in Texas was selling to read data off of 8" disks from a piece of industrial hardware. Back then the software was given to us by mailing it out on a floppy disk. :-) I got the latest changes, put the disk in the drive, ran it and... immediate crash. It didn't even start up. The fellow had never even bothered to execute his code before sending it to us (and he was paid quite a lot for these changes). A phone call and then another three days to receive the changes in the mail. The "it compiles so it must be correct" attitude has been around for a long time.

I also remember in the 1990s in college I asked a friend if his program was working. He responded "It works, it just doesn't give me the right answers". :-) Of course I followed up on this comment and he went on to explain that by "worked" he really meant it compiled.

5

u/le8ip9pu Jan 25 '15

I am not saying that compilation is supposed to replace later stages of testing (execution being the first of them). It just catches trivial errors earlier and more effectively. Many people consider catching bugs ASAP, a very important thing.

On the other hand, only in an ideal world you always have the full release cycle to apply and correctly verify a change. Sometimes (and this situation should be as rare and exceptional as possible) you have to jump into cowboy's boots and fix a critical crash on a living organism - because everything is better than system not working at all. It is very unpleasant experience when you fix/work around a sophisticated crash and instantly introduce another, trivial one, which you should catch during basic verification.

Of course, lints and excessive regression tests may sometimes be enough to catch it.

1

u/an_actual_human Jan 25 '15

Regarding the bit about "tab-completion": It is like an artificial leg. You can move when you have it but what happens if you lose it?

Why would that ever happen?

2

u/le8ip9pu Jan 25 '15

Maybe you have to solve an issue which occurs only when software is deployed on staging server (no special software installed only for development purposes)? Or even worse, you have to hotfix critical crash on a production system (no time for even basic release cycle)? Or maybe you are at the client environment and have no access to your normal set of tools? Or maybe you got a very well paid work at a brain dead corporation where you have to beg for any additional utility installed on "your" desktop and you wait for it for months?

I don't know, assuming that you will always work in your complete and perfectly setup development environment is like a wishful thinking for me. Isn't it better to have less tools on which you are critically dependant?

1

u/an_actual_human Jan 26 '15

All valid arguments, but they work just as well or better for C / C++.

3

u/Lucretiel Jan 25 '15

Ok let's get one thing straight here: memory leaks in C++ are a solved problem. There is no reason- ever- that a modern C++ developer should have to struggle with memory leaks. The standard containers provide basically all the dynamic (vector, string, list, map) and static (std::array) memory needs. The smart pointers in <memory> provide 0-overhead (unique_ptr) or minimal overhead (shared_ptr) memory management. Rvalue references and move semantics mean that there should never again be a border case of memory ownership or loss, and that all this safety can be enforced at compile time.

5

u/Steve_the_Scout Jan 25 '15

Not sure why you're getting downvoted (besides maybe people don't like hearing about things C++ does right). Everything you mentioned, combined with RAII if you ever decide to do manual memory/resource management, basically allows C++ to have the benefits of a runtime garbage collector with very few of the downsides, and even then it's only if you decide to make use of it- you don't pay for what you don't use (and in some cases, you don't pay for what you do use!).

If you want to criticize C++ there are much better arguments to be made against it (I say this with C++ as one of my/my favorite language(s)).

8

u/Steve_the_Scout Jan 25 '15

I mostly program in C++ (I'd have to say modern C++ i.e. C++11 is my favorite language, actually) and have done a little Python from time to time and I've never had an issue picking it up, they're just different tools for different types of requirements.

The reason I don't use Python more isn't because it's more difficult for me to write safe code (anyone who can read, know how to debug, and test things properly can easily write safe code), it's for multiple reasons- it's slower, lacks explicit type information (and so relies on documentation to get things right when working with an external library) requires the runtime to be present and using the same version you wrote, and in general is slower to reason about when projects get large (multi-file, several hundred lines each type projects).

It's great for writing "glue" code that manages a few separate processes, scripting for actual scripting purposes (e.g. AI for a game), in general shorter projects or parts of a project that need to be more flexible.

4

u/ismtrn Jan 25 '15 edited Jan 25 '15

When it comes to C++, the type system isn't really that strict. There are still plenty of things you can fuck up.

The comparison with haskell is interesting, but I don't really buy it. If you can write something with static types, you can write the same thing without them, you might just end up with a few extra bugs the type system would have caught. I would argue that using a programming language with a strong type system regularly actually means you are better at not making type errors, even when there is no type checker, because you have trained yourself to think about types all the time.

1

u/Steve_the_Scout Jan 25 '15

When it comes to C++, the type system isn't really that strict. There are still plenty of things you can fuck up.

I mainly program in C++, and I have no idea what you're talking about. If you want to mess with types, you have to either define your own implicit conversions (i.e. constructor taking only one argument), or do explicit ones. Could you give an example of some type error only caught at runtime?

-1

u/ismtrn Jan 25 '15
Class *object = new Class();
Class *object2 = object;
delete object;

object2 no longer has type Class *, even though the language think it has. In my view Null pointer dereferences are also type errors. In general, as soon as you have references/pointers, keeping proper type safety is quite hard.

2

u/Steve_the_Scout Jan 25 '15

You have no idea what you're talking about.

  1. object2 has type Class* always and forever.

  2. delete doesn't make a pointer nullptr. Assigning nullptr to a pointer makes it nullptr. delete just means the pointer is now pointing to deallocated memory that might be used by something else.

  3. Why the hell would a nullptr be a type error? For one, a pointer to something is its own type, an integral type representing its location, alignment, and size (believe it or not). When you have a nullptr pointer, that just means it doesn't point to anything (or rather, points to a location that effectively means it points to nothing).

  4. Null pointer dereferences are considered logic errors, not type errors, despite your own personal view. Sorry to be the one to tell you, but your beliefs contradict that of most programmers.

  5. On the contrary, it's pretty hard to avoid keeping proper type safety. In order to go so far as to treat an int as a float, you have to explicitly call reinterpret_cast<float*>(&some_int); Now I don't know about you, but I don't randomly include that line unless my goal is specifically to treat an int as a float for some reason.

Now if we were talking about C I'd be more inclined to agree with you, but C++ was designed to have better type safety than C to the point that the designers gave up some compatibility in order to do so.

2

u/ismtrn Jan 25 '15

Null pointer dereferences are considered logic errors, not type errors, despite your own personal view. Sorry to be the one to tell you, but your beliefs contradict that of most programmers.

Type safety (the ability of the type system to prevent type errors) is defined as "progress + preservation". That is well typed expressions can be evaluated to something, and that something will also be well typed. I am not alone in this view at all. Google "safety is progress plus preservation".

Dereferencing a null pointer is well typed, but unsupported by the language i.e. it is not type safe. "Weakly typed" has no hard and fast definition, but I would argue that a language that languages lacking type safety are weakly typed.

object2 has type Class* always and forever.

delete doesn't make a pointer nullptr. Assigning nullptr to a pointer makes it nullptr. delete just means the pointer is now pointing to deallocated memory that might be used by something else.

I did not mean to imply that it becomes a null pointer. It is a dangling pointer. What I meant to convey is that "dangling pointers are type errors, and so are null pointers".

As you said, object2 is now pointing to deallocated memory that might be used by something else. So now you can have a Class *, pointing to something else. So when you dereference it you break preservation.

Why the hell would a nullptr be a type error? For one, a pointer to something is its own type, an integral type representing its location, alignment, and size (believe it or not). When you have a nullptr pointer, that just means it doesn't point to anything (or rather, points to a location that effectively means it points to nothing).

This is related to my other points. A null pointer become a type error when you dereference it, because dereferencing takes something from type X* to type X. null pointers breaks this.

2

u/Steve_the_Scout Jan 25 '15

Alright, this explanation is much more coherent than the one I initially replied to, and I agree on all points.

The difference between our opinions is that I personally consider only static type safety to "count"- if you're going to be playing around with memory addresses directly in your program at runtime, since C and C++ work at the machine level directly, there is absolutely nothing the language can do to check that without being built on some sort of virtual machine and having every pointer actually be a pointer to a pointer etc., at which point you lose quite a bit of efficiency (in terms of speed and memory). It's understandable when the language is built specifically for the purpose of speed and efficiency.

That is, I believe type safety should only be considered when looking at language constructs and not methods of circumventing them. Using the deleted pointer example, it is undefined behavior to dereference a deleted pointer (same for a null pointer). The language says absolutely nothing about what happens when you try to do that. Therefor, in my opinion, it shouldn't even be considered in a discussion on type safety of the language. If the discussion were on the type safety of individual programs written with the language, it would make sense to mention it.

It would make more sense (in my opinion) to consider runtime type safety for other languages which do have defined behavior for what happens when you dereference a garbage collected or otherwise "deleted" pointer at runtime, for example.

2

u/teradactyl2 Jan 25 '15

By the time my C++/Java program is done compiling, I've already executed my python program, found the error where the line was, fixed it, and run it again.

Am I missing why people bring up static types up so often? I've never had a big issue with types or wasted more than a few seconds dealing with them.

16

u/rgrasell Jan 25 '15

Dynamic types can really bite you. What if your program runs for 10 minutes before it hits code with a type issue? That happened to me a few times on a crypto assignment in school. It is my fault, but programming languages are tools, and tools are supposed to help us.

6

u/bslatkin Jan 25 '15

Where were your tests?

12

u/cybercobra Jan 25 '15

Static type systems find type-related errors without writing any testcases.

1

u/cavallo71 Jan 25 '15

Never seen that, maybe you've found the holy grail of compilers/languages :D

-2

u/alcalde Jan 25 '15

They also require writing a lot more boilerplate code... and "interfaces", and "generics", and "function overloading", and all sorts of other scaffolding just to get around the static type system that users praise for "helping" them. Oh, and then there's the casting, which defeats the only benefit of static typing while leaving you with all the drawbacks.

Only a tiny subset of potential type errors are found by conventional static typing.

7

u/rgrasell Jan 25 '15

Sometimes you're just hacking something together! The code I was writing was just looping through a large encrypted file and collecting statistics on the bytes. Testing would have helped for sure, but a type system would have also been nice :)

2

u/cavallo71 Jan 25 '15

Sometimes you're just hacking something together!

That's the point: sometime, down the line you discover there's no need for the code: now finding that in a verbose languages it will cost (waste) hours/days.

-2

u/laebshade Jan 25 '15

Yep, where are his unit tests?

5

u/[deleted] Jan 25 '15

Dynamic typing can add a lot of flexibility to your program. In Java I often have to write a dedicated object to set up a flexible return value from a method when in Python I could easily return something of differing types and then set up a simple conditional and test case.

2

u/rgrasell Jan 25 '15

That's very true. There are lots if pros and cons. Personally, I like a strict type system more. But I do love Python for some things!

4

u/ubernostrum yes, you can have a pony Jan 25 '15

Please to define "static types" and "dynamic types".

(I think you may discover that the amount of stuff your "static" language actually puts off till runtime -- and the lack of complete information it works with until then -- may surprise you)

1

u/codygman Jan 25 '15

Would you mind giving some examples if possible for "conventional" statically typed languages (Java, C#, C++) as well as examples for languages with more advanced type systems (Haskell, Ocaml, Scala, ML, Idris)?

3

u/ubernostrum yes, you can have a pony Jan 25 '15 edited Jan 25 '15

Well, the classic example tends to involve what Java people call runtime polymorphism. Suppose you're writing a game, and the game has certain creatures, and some of them can be tamed, and some of those can mounted and ridden, and one of those is a horse.

You might end up with a hierarchy of classes like (of course, this would be poor design, but let's go with it for sake of example):

  • Creature

  • TameableCreature extends Creature

  • RideableCreature extends TameableCreature

  • Horse extends RideableCreature

It is entirely possible that, at compile time, the most specific type that can be deduced for a particular value will not be Horse, but rather RideableCreature or TameableCreature or even just Creature.

And this isn't just an artifact of the explicit type labeling in a language like Java; type-inferring languages can have the same problem of only being able to get to a relatively generic type.

This may not seem so bad, but at the very least it has performance implications (supporting runtime polymorphism isn't free, and quite a bit of work has gone into optimizing runtimes to use heuristic cheats and runtime type checks and guards to speed things up). And it also means that the compiler doesn't necessarily understand the code the way the developer does, which means the "if it compiles it's correct" mentality can be dangerous.

And that's just sort of the tip of the iceberg in terms of things people don't think about when making wild claims about static type systems.

For an example of consequences -- albeit in a slightly more primitive form -- see the current discussion of the Super Mario World "credit warp" exploit. The root of it is code which undoubtedly passed static type checks but still jumps off the end of an array and reads unintended memory, thanks in part to an attempt at runtime polymorphism :)

1

u/Steve_the_Scout Jan 25 '15

Alright, but Java is still a dynamically compiled language, so it still works a bit differently than e.g. C++ (the language actually mentioned in the article).

In C++, your runtime polymorphism cost is essentially nil- a class that has any virtual methods gets a virtual function table generated which is really essentially just a static instance of a struct containing named function pointers (i.e. there's only one for the whole class). When a virtual method gets called, the given function name is looked up in the struct, the pointer is dereferenced and passed whatever parameters it's supposed to get passed. For the classes that inherit from them, their virtual function tables contain the parent class's table at the very top (with modified function pointers if the derived class overrides them) and any new virtual functions.

So the cost is just however many accesses for the level of derivation, and a dereference (with regular function call costs). Basically negligible cost, totally safe (I mean, unless you somehow find a way to search raw memory for that table and directly modify the exact pointer you want to, somehow, but that goes true for any language), and nothing gets put off until runtime.

0

u/ubernostrum yes, you can have a pony Jan 26 '15

C++ is a bit different, yes, but making claims of static typing about a language with stupid pointer tricks is a whole separate can of worms -- C++ certainly can be statically typed, if you accept "pointer to god-knows-what" as a valid type for everything in the program :)

10

u/0xjake Jan 25 '15

C++ is faster than Python partially because the static typing allows the compiler to make aggressive optimizations. Another part is because C++ doesn't do checks that aren't specifically requested. For instance, in C++ you can read past the end of an array because it doesn't check every array access for validity. In Python you can't because you'll get an exception. All of Python's checks come at a price.

No matter how great you are at optimizing Python code, it will never be as fast as is possible with C/C++.

6

u/jjdmol Jan 25 '15

Also Python's flexibility comes at a price -- its memory layout basically is pointers to objects all the way (PyObject). In C/C++, data is consecutively stored as much as possible, causing far fewer cache misses.

Since memory I/O is a significant bottleneck for code nowadays, Python's memory layout gives a performance hit by design.

1

u/[deleted] Jan 25 '15

You might get to V8 speeds if you hire Lars Bak or something.

1

u/[deleted] Jan 25 '15

Meh, if python had a compiler it could be fast.

2

u/0xjake Jan 25 '15

Python does have a compiler. As I mentioned above, it performs a lot of extra checks and bookkeeping. I am admittedly not an expert on Python's execution and memory structure but I can guarantee you that the language has inherent inefficiencies that simultaneously promote ease of use and computation overhead. This is why it is outperformed by C++ regardless of whether you interpret or compile.

1

u/[deleted] Jan 26 '15

Sure, but PyPy exists. A Python compiler could do what PyPy does at compile time for the whole program (instead of just the hot path). Sure, it would need a fallback, but generating machine code that does the same thing as the interpreter is also a potential for more performance.

4

u/[deleted] Jan 25 '15

You forgot to take both aspects into the equation here:

  1. How long does it take to write the code?

  2. How long does it take to run the code?

In order to become more productive, you want to minimize the sum of 1. and 2. (Okay, to be fair, you don't have to sit there idle while your code runs but can start working on sth. else)

Often, I write some optimization algos (mostly machine learning stuff) that takes ~days to run. If I only need to run it once, I am probably better off leaving this as is in Python before I spend half a day rewriting it in C++. However, if I use this code more frequently, it only makes sense to use static languages or Cython

1

u/batiste Jan 25 '15

You van also add

3. How much time it take to read and comprehend the code.

As program grow bigger they need more and more maintenance.

4

u/[deleted] Jan 25 '15

But how long does your test suite takes to run? ;)

-2

u/teradactyl2 Jan 25 '15

I think tests are a waste of time. But I only work on small projects so it just depends on what you're doing.

4

u/alcalde Jan 25 '15

I don't get it either. In all my years of using statically typed languages, I'm not sure I've ever tried to pass "spaghetti" into the square root function instead of a number. I've got epic tales of bugs (including one that saved a company) but absolutely nothing that revolves around types. It's logic bugs that will have you lying awake at night staring at the ceiling trying to figure out what's wrong.

3

u/codygman Jan 25 '15

There is truth to this, but more advanced type systems allow you to encode logic in your types. Since that logic is now encoded in the types logic errors become type errors.

1

u/alcalde Jan 26 '15

I don't have a lot of familiarity with the exotic languages that do this, but then isn't that really just writing lots of tests in your code and calling it "types"? Does it really save lots of typing or is the amount of extra work involved equal to putting in lots of asserts and other code into a conventionally typed language program?

2

u/codygman Jan 26 '15

I don't have a lot of familiarity with the exotic languages that do this,

Me neither, just with Haskell ;)

but then isn't that really just writing lots of tests in your code and calling it "types"?

I guess you could look at it that way sort of, but I think it would be more accurate to say the type checker gives you a succinct and declarative way of describing these tests. If you don't write the "tests" (types really, but sticking with your metaphor), the compiler writes one for you.

More about encoding logic in types and other possibly tangential stuff...

Instead of writing tests to validate a data type such as an object, dictionary, or some sort of structured list is valid, one popular Haskell* approach is to make it impossible to create or otherwise represent invalid states. The flexible type system lets you go a lot farther doing this than you may think is possible (dependently typed languages such as Idris go farther). There is even a term coined for this method: "Correct by construction".

It's kind of the diametrical opposition to duck typing. There is no if it looks like a duck, I only allow making certain types of ducks and I know exactly what they look like and how they can fail is enforced by the type system via Maybe/Optionals.

When I say "enforced by", I mean if you don't handle the error states the compiler complains or errors when using -WError (or anything that implies -fwarn-incomplete-patterns). By default you get a warning when compiling that a pattern isn't matched.

I assumed some background with my explanation, let me know if you need more direction on specific pieces.

1

u/I_Like_Spaghetti Jan 25 '15

If you could have any one food for the rest of your life, what would it be and why is it spaghetti?

2

u/Steve_the_Scout Jan 25 '15

In my experience, the amount of time it takes to find an error in a "dynamic" language is like an O(nlogn) worst-case (n being the number of lines), while in a "static" language it's more like O(n).

While it may be faster to find errors in "dynamic" languages when the projects are small, as it gets larger and larger, it becomes increasingly easier to find errors in the "static" language.

Also, I doubt you can run a script, notice an error, dig through the section you believe it's in, fix it, and run the whole thing again in the time it takes to compile a project of comparable size in Java/C++, which would probably be compiling in < 500 ms if I'm understanding the type of Python project you're talking about. You know, a small, sub-thousand line single file project that scripting languages are typically used for.

0

u/le8ip9pu Jan 25 '15

By the time my C++/Java program is done compiling, I've already executed my python program, found the error where the line was, fixed it, and run it again.

And found another error, two lines later :) And another one. And another one. The guy compiling his Java code catches them all at once and can concentrate on logical correctness of the program from the moment it executes (less of context switching for him).

1

u/teradactyl2 Jan 25 '15

So it's for sloppy programmers? How many errors do you write?

2

u/JavaDroid Jan 25 '15

Very likely about the same as you.

1

u/le8ip9pu Jan 25 '15

So it's for sloppy programmers? How many errors do you write?

In case of the first implementation or any significant code reorganization (I don't talk about elementary refactoring actions here)? From some to much. And most of them are trivial, one-second typing/syntax errors fixes.

I just don't have to loose time looking for potential missing-semicolon class errors. The compiler (or a lint) will catch them for me.

-5

u/derpderp3200 An evil person Jan 25 '15

Another downside of Python is its minuscule performance. Sure it doesn't matter much for simple batch processing, but once you try to do anything more than that, you've got to microoptimize your code really, really soon.

8

u/riffito Jan 25 '15

I've built a several megabytes project in Python in my last job (7 years at it). I HAD to microoptimize exactly ONCE (the rest of the times I was just being silly).

Granted, most operations were I/O bound (as plenty of things are). Performance was never an issue. The one time I had to optimize was dealing with huge files, and I would have made the same optimizations in C/C++ if I where using that.

-3

u/derpderp3200 An evil person Jan 25 '15

Well, I take it you haven't used it on a lower end machine, then.

1

u/riffito Jan 25 '15

I live in a third world country, so... all the machines I used are low end :-)

An AMD K7 @ 900 MHz with 256 MB of RAM, was my main machine until two years ago.

8

u/ixokai Jan 25 '15

Overly simplistic.

Yes, Python is slower then C++, but its more then fast enough for huge domains of applications that are more advanced then 'simple batch processing'.

Not to mention, there are other implementations then CPython. PyPy is particularly promising.

3

u/alcalde Jan 25 '15

but its more then fast enough

I've actually had "faster is always better" thrown at me in an attempt to rebut that point on multiple occasions. :-( That and their domain, whatever it is, is more complex than anything you do so it couldn't possibly be fast enough.

2

u/alcalde Jan 25 '15

you've got to microoptimize your code really, really soon.

Which you can do with time left over thanks to all the time you saved writing it.

And that's not taking into account the contingent of developers who prematurely optimize everything anyway, even with compiled languages.

0

u/[deleted] Jan 25 '15

[deleted]

0

u/[deleted] Jan 25 '15

[deleted]

4

u/wmil Jan 25 '15

He's not talking about speed. If you're dealing with a task that's I/O bound instead of CPU bound then C++ performance is unimportant.

0

u/[deleted] Jan 25 '15

[deleted]

6

u/alcalde Jan 25 '15

You'd be surprised though how often this argument is used. It's a classic. Assembly developers said that a compiler could never be as fast as their hand-written assembler. GOTO advocates said that the overhead of functions would kill performance. And today compiled language fans continue to call Java and C# and Python impossibly slow (even when their favorite language gets beaten in SciMark by Java). And if you don't prematurely optimize the heck out of your code you're "lazy" or "too stupid". They also brag if their language allows inline assembly. When you tell them that developer time is more expensive than computer time, they tell you that unlike their old-school skills, kids today are just ignorant and stupid and don't know how to manage memory like they do... which they do, manually, in their software, for reasons they can't explain other than that they can do it "better" than any garbage collection algorithm.

Heck, on Stack Overflow one Delphi user wrote a routine in assembly. I asked him how long it took to write: THREE FULL DAYS. But he just knew it was going to be a bottleneck, so he did it in assembly. Of course he never wrote it in Delphi first so he had no real idea if his code was faster at all. :-( But you still can't tell these kind of people that developer time is important. I had one guy yell at me online that my code might be simple, but HE was the corporate director of such-and-such and needed to produce 132 reports and there was just NO WAY that Python could possibly spit his reports out quickly enough. He'd never even seen a line of Python and knew nothing of the language before the conversation but as soon as he learned it wasn't compiled he didn't want to hear anything else about developer time or premature optimization. His work was too important and he was too special a snowflake to sully himself with dynamic code.

-7

u/Jabulon Jan 25 '15 edited Jan 25 '15

i didnt like this article at all.

python is like using a shovel.

c++ is like designing the shovel, using it, redefining it, and inventing a new one, thats even better.

plus you own the creation

3

u/tilkau Jan 25 '15

python is like using a shovel.

c++ is like designing the shovel, using it, redefining it, and inventing a new one, thats even better.

plus u own ur own creation

What are you trying to say here? That reinventing the wheel is a positive outcome?

-5

u/Jabulon Jan 25 '15

if its ur own custom wheel, yes.

1

u/[deleted] Jan 25 '15

And you own spelling system.

0

u/[deleted] Jan 25 '15

[deleted]

10

u/eigma Jan 25 '15

Why would you want to Why would you want to Why would you want to Why would you want to Why would you want to Why would you want to Why would you want to Why would you want to

1

u/[deleted] Jan 26 '15

more control over spacing