Live data from Hacker News

Python, as Reviewed by a C++ Programmer

sgh1.net

51–60 of 79 posts

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

#51
post #44

Earlier quoted context omitted.

In Java, this code will print the string representation of Lassie: Dog toby = new Dog("Toby"); public static void replaceDogWithToby ( Dog d ) { d = toby; } public static void main ( String[] args ) { Dog d = new Dog("Lassie"); replaceDogWithToby(d); System.out.println(d); } In Pascal, this will print Toby: var toby:tDog; procedure replaceDogWithToby ( var d:tDog ); begin d := toby; end; var lassie:tDog; begin {code…

I haven't done java in a long time so I won't try to debate the specific language level semantics with you (the same is true of pascal though it has been even longer), however assuming that your examples run and behave as you describe then what is going on in the java example is that 'Dog d' is passed as a reference to a reference, and then the value pointed to by the argument, i.e. the address of "toby," is replaced…

You got it wrong -- it starts as Lassie and stays as such after the function call (as "replacement" only affects the local (stack) copy of the pointer / refernce passed) :)

No fancy pointer-to-pointer business thus.

No idea what's going on Pascal, though, have to check out myself.

upd: upon checking http://wiki.freepascal.org/Variable_parameter , I suspect that here a pointer is passed underneath again, but the assignment now copies the bytes over into the pointed-to object.

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

#52

What is a "C++ programmer?" On any given day, I poop you not, I'm writing either c#, java, c++ or any number of scrpting languages (perl, bash, python, JavaScript). Do people seriously only use a single language these days!?

The main product I work on is written in C++, so I spend most of my day looking at\writing C++ and also learning as much C++ as I can. I do use other languages for some minor tasks (python and c# for some automated test stuff and devopsish kind of work), but i dont use those languages as much as C++ nor do I invest as much effort into learning the ins and outs of those languages. So I consider myself a C++ programmer because its what I put effort into

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

#53
post #44

Earlier quoted context omitted.

In Java, this code will print the string representation of Lassie: Dog toby = new Dog("Toby"); public static void replaceDogWithToby ( Dog d ) { d = toby; } public static void main ( String[] args ) { Dog d = new Dog("Lassie"); replaceDogWithToby(d); System.out.println(d); } In Pascal, this will print Toby: var toby:tDog; procedure replaceDogWithToby ( var d:tDog ); begin d := toby; end; var lassie:tDog; begin {code…

I haven't done java in a long time so I won't try to debate the specific language level semantics with you (the same is true of pascal though it has been even longer), however assuming that your examples run and behave as you describe then what is going on in the java example is that 'Dog d' is passed as a reference to a reference, and then the value pointed to by the argument, i.e. the address of "toby," is replaced…

You got it backwards. The Java snippet will print "Lassie". In Java, object names are references to objects, and passing them to functions copies the references, so of course modifying the formal parameter (d from replaceDogWithToby()) doesn't modify the actual parameter (d from main()).

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

#54

What is a "C++ programmer?" On any given day, I poop you not, I'm writing either c#, java, c++ or any number of scrpting languages (perl, bash, python, JavaScript). Do people seriously only use a single language these days!?

>Do people seriously only use a single language these days!?

Yes. In what bizarro world would one assume it's not so? By thinking that all development in the world resembles the company/work they do?

Tons of programmers only write C# or Java on Windows, and don't touch any shell scripting at all.

Tons of front-end developers only write Javascript.

Tons of statistics people only use R or 1-2 other statistics packages.

Tons of systems, drivers, embedded and OS people only C or C++.

Tons of game devs only write C++.

Tons of native mobile devs only write Swift, Objective-C or Java.

Some of them do occasional need to write some script to get something done, but it's hardly like the juggle between multiple languages.

And of course larger companies have specialized programmers for different domains, not some guy doing C++ AND admin stuff in shell AND some front-end JS etc.

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

#55
post #27

For me the biggest downside of dynamically typed lanaguages like Python and Javascript is that a small error in passing a function's parameters can propagate very far in a complex system. E.g. if you mistakenly interchange two consecutive function parameters, you could spend several hours debugging the effect several modules down. This is an error that a C++ compiler would catch right away.

Type hints can catch most of these mistakes.

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

#56
post #27

For me the biggest downside of dynamically typed lanaguages like Python and Javascript is that a small error in passing a function's parameters can propagate very far in a complex system. E.g. if you mistakenly interchange two consecutive function parameters, you could spend several hours debugging the effect several modules down. This is an error that a C++ compiler would catch right away.

There are solutions for this: flowtype for js or mypy for python (preferably with python3.5+).

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

#57
post #45
post #42

Earlier quoted context omitted.

Why do you jump between multiple languages all the time?

Not, OP, but I've worked for an ad-agency in a software-dev role. Their primary code base is written in Java, monitoring scripts in Python/Ruby, and tracking pixel code in JavaScript, so I would have days in which I coded in all 3, albeit for separate use-cases.

The ad agency couldn't afford different developers for the various systems?

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

#58

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).

Plus, many packages dynamically add members, stomp named parameters by wrapping stuff with kwargs, etc.

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

#59

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).

True, but very popular packages are usually battle tested and type errors are very rare in practice in those packages. Most bugs I've seen are not because of the lack of a static type system.

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

#60

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.

Or specifically to his example,

    from typing import Dict
    Foo = Dict[int:str]
    def __init__(self, name:str, balance=0.0):
        """Construct a Klass."""
        self.myfoos = {} # type: Foo
I _think_ that's right... close anyway
Post reply on HN