Live data from Hacker News

Python, as Reviewed by a C++ Programmer

sgh1.net

31–40 of 79 posts

Re: Python, as Reviewed by a C++ Programmer

#31
post #13

Earlier quoted context omitted.

No, no, no there is no "call by object." It is precisely this mistaken idea which muddies the waters, as you put it. Virtually all modern processors are stack-based architectures. All arguments to functions are passed on the stack or in registers. The only thing that can be placed onto the stack or into a register are numbers. Those numbers can represent either an actual value or an address in memory. The former is c…

It may be implemented as call-by-reference, but the semantics are not the same as what one thinks of when he hears that phrase.

In what way are they different?

Re: Python, as Reviewed by a C++ Programmer

#32
post #8

Earlier quoted context omitted.

Claims that Python/C#/Java etc., are call by value where the 'value' is the value of the pointer does not help. It only muddies things. There really ought to be different name for such call semantics and there is. Its called call by object. In C++ lingo call by const pointer comes closest: one cannot change what the pointer points to. However the pointed object may not be a const

Python is "pass reference by value", in C++ semantics.

Of course, because that is the only way to implement call by reference, i.e. to pass the reference as a value.

Re: Python, as Reviewed by a C++ Programmer

#33
post #5
post #4

Rust is better than C++ at “if it compiles, it works” and has a great package manager, so as far as the topics highlighted here go, Rust seems to be the best of both worlds.

One thing where Rust is rather lacking is the missionarism of some people in the community.

Seems naive for rust to try to persuade you to use their language when the right way is to launch under the aegis of a vertically integrated corporate platform. You need an HP, Sun, MS, Apple or Google foisting you on developers to have any shot at mainstream success.

Re: Python, as Reviewed by a C++ Programmer

#34
There are package managers for C++, such as Conan. Some IDEs like CLion have certain degree of integration with it.

https://www.conan.io

Then, some systems cannot afford to have garbage collection, because it usually runs in an unpredictable amount of time. Such systems are for example real-time systems. In those cases a language like C++ is a good fit.

Re: Python, as Reviewed by a C++ Programmer

#35
post #13

Earlier quoted context omitted.

No, no, no there is no "call by object." It is precisely this mistaken idea which muddies the waters, as you put it. Virtually all modern processors are stack-based architectures. All arguments to functions are passed on the stack or in registers. The only thing that can be placed onto the stack or into a register are numbers. Those numbers can represent either an actual value or an address in memory. The former is c…

Yes. But. Language designers go out of their way typically to hide this underlying truth, by for example implementing "call by value" when the type of the value is "Customer" or whatever.

It's specifically because high level language semantics often mask the underlying implementation that knowing that implementation is necessary for truly understanding what's happening. The situation you describe is call by reference, where the value passed on the stack or in a register is a reference to a customer object. You could pass a customer object by value (in languages that allow it, such as C++) by copying the structure verbatim onto the stack and addressing it there.

Re: Python, as Reviewed by a C++ Programmer

#36
The article doesn't mention type annotations described in PEP484 and added to CPython since Python 3.5.

It is possible to add annotations to your code like

    class Foo:
        x: int
        # ...
and then run type checks using `mypy`, so that for the annotated code probability of the "passes the checks => won't crash the runtime" case is much larger.

Re: Python, as Reviewed by a C++ Programmer

#37
post #13
post #8

Earlier quoted context omitted.

Claims that Python/C#/Java etc., are call by value where the 'value' is the value of the pointer does not help. It only muddies things. There really ought to be different name for such call semantics and there is. Its called call by object. In C++ lingo call by const pointer comes closest: one cannot change what the pointer points to. However the pointed object may not be a const

No, no, no there is no "call by object." It is precisely this mistaken idea which muddies the waters, as you put it. Virtually all modern processors are stack-based architectures. All arguments to functions are passed on the stack or in registers. The only thing that can be placed onto the stack or into a register are numbers. Those numbers can represent either an actual value or an address in memory. The former is c…

The confusion comes from the difference between references in Python and C++. In Python you can reassign a reference to a different object any time you want. In C++ you can't, and any attempt to do so modifies the referenced object instead. That fundamental difference makes it hard to understand the mechanics of Python using the terminology of C++.

Re: Python, as Reviewed by a C++ Programmer

#38
post #31

Earlier quoted context omitted.

It may be implemented as call-by-reference, but the semantics are not the same as what one thinks of when he hears that phrase.

In what way are they different?

In a classic call-by-reference, changes you make to the passed parameter are seen by the caller when the function returns. That doesn't happen consistently with Python.

Re: Python, as Reviewed by a C++ Programmer

#39

The article doesn't mention type annotations described in PEP484 and added to CPython since Python 3.5. It is possible to add annotations to your code like class Foo: x: int # ... and then run type checks using `mypy`, so that for the annotated code probability of the "passes the checks => won't crash the runtime" case is much larger.

Sure, but most python code isn't annotated, right?

So this might be helpful for code you wrote, but not anywhere it interfaces with other peoples' code (which, in my opinion, is probably the most important place to check for type errors).

Re: Python, as Reviewed by a C++ Programmer

#40
post #13
post #8

Earlier quoted context omitted.

Claims that Python/C#/Java etc., are call by value where the 'value' is the value of the pointer does not help. It only muddies things. There really ought to be different name for such call semantics and there is. Its called call by object. In C++ lingo call by const pointer comes closest: one cannot change what the pointer points to. However the pointed object may not be a const

No, no, no there is no "call by object." It is precisely this mistaken idea which muddies the waters, as you put it. Virtually all modern processors are stack-based architectures. All arguments to functions are passed on the stack or in registers. The only thing that can be placed onto the stack or into a register are numbers. Those numbers can represent either an actual value or an address in memory. The former is c…

processors and architectures have nothing to do with this. This is a matter of semantics. The prolem stems from different communities appropriating the same english word to mean subtly different things, hence my clarification "in C++ lingo ..."
Post reply on HN