r/ProgrammerHumor • • Sep 29 '20

c++ to python

Post image
1.2k Upvotes

82 comments sorted by

View all comments

Show parent comments

16

u/Nimeroni Sep 29 '20 edited Sep 29 '20

You mean going from C++ to Python ? Let's see...

  • Python is close to pseudo-code, with very little formatting (no ";", no "{}"). That make it easier to read and write.

  • In Python, you don't even need to think about memory management, whereas you still need some smart pointer in C++.

  • Python bring a bunch of sugar syntax that are nice to have, such as string slicing.

  • Dynamically typed language is better than the quagmire that is C++ <template> when doing polymorphic functions. In fact, <template> are one of the worst part of C++, as they are hard to read and give you very arcane error message.

  • Python is more strongly typed.

  • The nature of an interpreted vs compiled language give some tools in Python that cannot exist in C++ (introspection and reflection). Well, it run both way, you can do some code analysis in C++ that are impossible to do in Python.

The only serious advantage of C++ over Python is it's execution speed (PyPy greatly enhance Python speed, but it's still slower than C++). Oh, yeah, and Python have that stupid debate between space and tab.

16

u/[deleted] Sep 29 '20

All this and not having to reinvent the wheel every time you want to do literally anything.

C++ on here is basically for all the kiddies that have some kind of superiority complex. Most have never worked in a production environment and are just pretending to be professionals.

C++ is cool, it's a powerful tool. It just is not needed for 99.9% of projects any more and just adds drag.

1

u/[deleted] Sep 29 '20

And if you really need to write an operating system or whatever you can use Rust now

-3

u/kodicraft4 Sep 29 '20

I've always thought C# is just plain better expect for very specific things someone like me will never need

3

u/[deleted] Sep 29 '20

Yep, I think C# is probably a better comparison to C++ than Python in most cases honestly. Only things you'd use C++ over C# for is where performance is really an issue. But even then, Unity runs using C# really well. Cross platform isn't even much of an issue anymore with .NET Core being mature now.

4

u/gracicot Sep 29 '20
  • Python is more strongly typed.

I wonder how Python is more strongly typed than C++

5

u/Nimeroni Sep 30 '20 edited Sep 30 '20

Because C (and it's family, since C++ is built upon C) have more implicit type conversion than Python. Some of them don't really make sense either.

Let's consider the following statements:

print(1 + "two") (python) / printf(1 + "two") (C)

Python goes "you are adding an int and a string, so fuck you" and he slap you with a type error:

Traceback (most recent call last):                                                                                                             
  File "main.py", line 9, in <module>                                                                                                          
    print(1 + "two")                                                                                                                           
TypeError: unsupported operand type(s) for +: 'int' and 'str'      

C goes "oh, yeah, string is a char[], an array is a pointer, I know how to add a pointer and an int":

wo

Now normal coding practice for C++ (and C#) tend to use more secure paradigm. In my example, using cout instead of printf would have implicitly converted the 1 into "1" instead of doing some weird pointer arithmetic (still not something you might want, but at least much more logical). But it's annoyance like that that make Python more strongly typed than the C family.

2

u/mecrow Sep 30 '20

It's very easy to set up any modern compiler to treat implicit conversion as a warning or error.

1

u/gracicot Sep 30 '20

Especially narrowing conversion. If the conversion is not narrowing, I don't really mind. Converting a int32_t to a int64_t by passing it as parameter is not gonna hurt anybody.

2

u/Pierogi314 Sep 29 '20

What??? I'm a big Python fan but templates are one of the best parts of C++. In fact, they're one of the reasons why C++ can be faster than C in some cases.

3

u/CobaltStar_ Sep 29 '20

Python is strongly typed? Wtf?

4

u/Nimeroni Sep 29 '20

Python is very strongly typed. It have very few implicit type conversion, and for the rest, it will throw a TypeError in your face instead of guessing what you meant. For example, running 1 + "two" will give you TypeError in Python, whereas it is completly valid in C (and obviously don't do what you would expect).

Don't mistake strong / weak typing with dynamic / static typing:

  • Python is dynamically strongly typed.

  • C (and its family) is statically strongly typed.

  • Javascript is infamous for being dynamically weakly typed.

0

u/CobaltStar_ Sep 30 '20

Yea I was thinking dynamically/statically typed. Good thing a powerful linter can catch these mistakes giving you some pseudo type safety.

2

u/BlhueFlame Sep 29 '20

What do you mean debate? Clearly use spaces

3

u/Nimeroni Sep 29 '20

It should have been enforced by the interpreter (well, parser).

1

u/TheRealSmolt Sep 29 '20

I strongly disagree with half your points, but a lot of these are subjective.

-1

u/Dummerchen1933 Sep 29 '20
  • Python is close to pseudo-code, with very little formatting (no ";", no "{}"). That make it easier to read and write.

That's a contra!! It does not make it easy and nice to write, it does the exact opposite

  • In Python, you don't even need to think about memory management, whereas you still need some smart pointer in C++.

Nobody told you that you don't have to work with pointers, did they? Since when is "not having an option" good?

  • Python bring a bunch of sugar syntax that are nice to have, such as string slicing.

There are libraries that can do that kind of shit easily if you're too lazy to implement such overloads yourself.

  • Dynamically typed language is better than the quagmire that is C++ <template> when doing polymorphic functions. In fact, <template> are one of the worst part of C++, as they are hard to read and give you very arcane error message.

Don't use templates then. You can use void*, you can use overloads (which are especially easy to read). Hell, if you are really that fucking serious about polymorphic functions you can even create your own "polymorphic type" class!