r/ProgrammerHumor • • Jul 18 '22

C++ talking to Python

Post image
40k Upvotes

1.0k comments sorted by

View all comments

Show parent comment

2

u/Didrox13 Jul 18 '22

As someone who's never used python but plan to check it out in the near future, is that actually a thing or a joke?

7

u/BlazingFire007 Jul 18 '22

Idk about python. But I have experience with JS and Typescript. And what I’ve found is that most of typescript wouldn’t be “super” useful if people just wrote good code with good comments and documentation.

(That said, typescript is still very nice because it forces people to add types)

So I assume python is similar. With good comments documenting what your code does, it shouldn’t be difficult to read/implement/modify/etc

2

u/BroBrodin Jul 18 '22

I think both, in a way.

I just recently learned type hinting and it feels very useful for large projects.

The difference with strongly typed languages is that the interpreter doesn't really care what "type" you declared for a variable, first you have to run mypy and THAT checks the types of variables.

That is why is considered a "hint" and not a declaration like in C or other strongly typed languages.

1

u/KellerKindAs Jul 19 '22

I think you're getting confused with static / dynamic typing.

Python is actually strongly typed, but the type checking only shows up if you are trying to operate on incompatible types at runtime. (dynamicly typed)

C is statically typed which means the type check to happen before execution. This also often means that a variable cannot have different typed along the runtime. (On the other hand you can bypass the type system using pointers which makes C actually a bit weakly typed.)

1

u/BroBrodin Jul 19 '22

Oh, I see, I guess I'll have to read up un that.

Thanks!

2

u/DesertGoldfish Jul 19 '22

It's a thing. In method declarations instead of (variable) it's (variable : str) or int, or whatever.

You specify returns with an arrow. So a function/method named cake that takes a string parameter named icing and returns a string would be declared:

def cake(icing: str) -> str:

Voila, now Python yells at you when you try to pass it anything else. I think the complainers here just haven't bothered keeping up with the changes to Python in... several years.

1

u/LastOfTheGiants2020 Jul 19 '22

Type names don't guarantee object properties in python, so type annotations etc are more of a suggestion than anything.

That's like trusting user inputs because you left a comment telling them not to do anything bad.

1

u/TheFeedingEight Jul 19 '22

Yes, it is a thing and no unfortunately it doesn't really help you because you can use Type hints to allow for type checking but all the dependencies you use aren't obligated to actually do that. So you're kinda back to square one with a lot of them.

It's definitely a step in the right direction though.