News for hackers. Stuff that matters.
Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @05:22PM (#17570397)
For some reason, I never liked Python. I don't know... its classes look weird with all those self, __init__, etc. The __init__.py for packages is also a strange choice to me. It has classes, but no interfaces. The language is even slower than PHP. Does Python have type hinting? What all the hype is about? I just don't get it.
Re:Python has brought computer programming to a vast new aud... by cm2187 on Thursday July 19, 2018 @07:19PM (#17571208)
Agree. I can't understand people praising the lightness and readability while at the same time stuffing their code with underscores. Also it seems often inconsistent. Like some functions are used as prefix (len(x)) while other are suffix (x.clear()), I assume for historical reasons. Or like the "b if a else c" which reverse the logical order (you evaluate the condition then you can tell if you go one way or another). Same for [x for x in list], the Linq approach is more logical (for x in list select x).

I understand why people like it. And I like scripting languages, it's nice to find a piece of script somewhere and being able to edit it directly having to find the correct source code and recompiling. But it has lots of shortfalls and quirks too (multi-threading, dependencies management, deployment, no refactoring/static checking, python 2 vs 3, 50 different kinds of strings, APIs full of abbreviations which probably meant something to their creators 20 years ago, etc).

It's a nice language, among others. Not an "ultimate" language by any means in my opinion.

Re:Python has brought computer programming to a vast new aud... by meowface on Thursday July 19, 2018 @07:53PM (#17571387)
Those are all valid gripes, but for most use-cases (where performance isn't critical), I haven't found anything better than Python yet. Ruby is an aesthetically nicer language and doesn't have those issues you mentioned in the first paragraph, but the Python ecosystem is amazing and better than Ruby's IMO. And compared to most other languages Python still feels very clean, terse, and pretty to me.

>multi-threading

Most interpreted languages have a GIL, sadly. But yes, this is annoying.

>dependencies management

Annoying, but resolved in part with new projects like pipenv, and also still a big issue for other popular languages.

>deployment

Also helped by pipenv, and Facebook's xar (https://github.com/facebookincubator/xar) is new but looks promising.

>no refactoring/static checking

It's a dynamic language. But this is now possible to some extent with type hints.

>python 2 vs 3

They did screw this up, yeah. Python 3 fixes a ton of issues and is a better language, but the backwards incompatibility created way too many problems.

>50 different kinds of strings

There are 2 kinds: bytes and unicode.

>APIs full of abbreviations which probably meant something to their creators 20 years ago

Can you give some examples? This is an issue in a few of the old standard libraries, but even then it's not too common. This is way, way more prevalent in PHP and C than in Python.

Re:Python has brought computer programming to a vast new aud... by cm2187 on Friday July 20, 2018 @03:52AM (#17573210)
> There are 2 kinds: bytes and unicode.

No I mean single quote, double quote, triple double quotes, r prefix, u prefix, f prefix, combinations, etc

> Can you give some examples

The one I have in mind is the os library, which I use a lot (like why st_mtime?).

Re:Python has brought computer programming to a vast new aud... by meowface on Friday July 20, 2018 @03:02PM (#17577879)
>No I mean single quote, double quote, triple double quotes, r prefix, u prefix, f prefix, combinations, etc

These are all extremely helpful, though. All of those quote combinations are equivalent, and can be used to prevent having to escape quotes within the string. The u prefix is gone from Python 3, r is a raw string (which many other languages have, like C#'s @ prefix), and f is a new feature which generated a lot of debate but basically allows interpolation similar to Ruby and ES6.

>The one I have in mind is the os library, which I use a lot (like why st_mtime?).

That's the one I thought you were going to bring up. I agree the naming conventions in the os module have a lot of warts. The function names could use a refresh.

Re:Python has brought computer programming to a vast new aud... by eesmith on Saturday July 21, 2018 @04:29PM (#17583705)
You wrote "These are all extremely helpful".

I disagree, in that I think they are only somewhat helpful. If we only had double-quoted strings, and not single-quoted strings, then I think there would be little difference to the language or its quality of use.

Going back to the question of "why st_mtime?" - that's because os.stat() is a wrapper around the POSIX stat(2) system call. My FreeBSD man page defines it as:

   st_mtim   Time when file data last modified.  Changed by the mkdir(2),
             mkfifo(2), mknod(2), utimes(2), write(2) and writev(2) sys-
             tem calls.
The Python implementation of stat for MS Windows is basically doing its own emulation layer for POSIX compatibility.
Re:Python has brought computer programming to a vast new aud... by noname120 on Thursday July 19, 2018 @05:49PM (#17570613)
Yes, Python has type hinting since 3.5[1].

https://www.python.org/dev/peps/pep-0484/

Re:Python has brought computer programming to a vast new aud... by yen223 on Thursday July 19, 2018 @06:42PM (#17570986)
To be clear, Python has had type annotations for a while now, but the default CPython interpreter doesn't use that information to optimise performance.
Re:Python has brought computer programming to a vast new aud... by UncleEntity on Thursday July 19, 2018 @07:12PM (#17571174)
The CPython interpreter does very little to optimise performance because, as the reference implementation, they strive for simplicity of implementation.
Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @06:39PM (#17570965)
No interfaces?

    from collections.abc import Sequence
Slower?

    import numpy as np
Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @06:51PM (#17571040)
You're referring to duck typing, which to me contradicts the Zen of Python: Explicit is better than implicit.

> Slower?

Yes, overall the language is slower than PHP. It's not a bad thing, just the result of benchmarks. It doesn't mean one should stop writing Python.

Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @06:57PM (#17571074)
No, I'm not referring to duck typing. An abstract base class enforces the interface. Explicitly.

Some benchmarks might be slower, but I don't know anyone doing high-performance computing with PHP. I do see the topic at Python conferences.

Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @07:27PM (#17571255)
abstract base class != interface
Re:Python has brought computer programming to a vast new aud... by kjeetgill on Thursday July 19, 2018 @09:44PM (#17571836)
Let me help you two out:

ABCs are like interfaces in they can be used to enforce properties (like the presence of particular functions) of a class that extends/impliments them; BUT, interfaces can also be used to enforce a particular variable always refers to something with certain properties (without worrying about what it is concretely).

Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @08:06PM (#17571443)
Right, they're even better than Interfaces: both a contract and a partial implementation. No need for an "Interface" when you've got ABCs.
Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @08:50PM (#17571633)
I see what you mean, but to me abstract classes are distinct from interfaces. Abstract class answers the question "what an object is?", whereas an interface is about what the object can do.
Re:Python has brought computer programming to a vast new aud... by dragonwriter on Thursday July 19, 2018 @09:51PM (#17571861)
The distinction between what an object is and what an object can do is an artifact of bad object modelling (specifically, single inheritance OOP.) Any “can do” set has an exactly corresponding “is a thing that can do” set, so there is no logical need for different constructs to model “is” sets and “can do” sets.
Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @10:44PM (#17572058)
Sounds like a controversial statement and I'm not sure how universal true this is. I'll give it a thought. Thank you.
Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @11:03PM (#17572136)
Another way of explaining this is that Interfaces are Java's way to compensate for the lack of multiple inheritance.

There's more than one variety of inheritance. Both Python and Java chose implementation-inheritance, meaning that the children get to use not only the interface, but also the implementation of the parent.

For some reason, I'm not quite sure why, the Java folks thought that multiple implementation-inheritance would cause too many problems. They still recognized it's useful, so they provided a 2nd form of inheritance that supports multiple parents, interface-inheritance.

There are more varieties of inheritance that a language could choose, for example, prototypes like JavaScript.

Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @11:51PM (#17572307)
Yes, I'm familiar with all of that, but isn't the Diamond of Death [1] is the exact reason why they did it?

[1] https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamo...

Re:Python has brought computer programming to a vast new aud... by xapata on Friday July 20, 2018 @12:33AM (#17572476)
People read left to right. That solves all the trivial problems. The harder problems were always vocabulary problems not programming problems.

The trouble is that English, and other human languages, have never been as clear or specific as we assume they are when we start writing code. For example, what is a bird? It's a thing that flies? Oh, ostrich, penguin...

All these supposed OOP inheritance problems aren't unique to inheritance but to the translation of human language to programming. There's no need to exclude multiple inheritance, because nearly every aspect of programming is afflicted.

Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @09:20PM (#17571760)
That distinction is unnecessary.
Re:Python has brought computer programming to a vast new aud... by danso on Thursday July 19, 2018 @05:29PM (#17570451)
What other language are you comparing Python's flaws and tradeoffs to?
Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @05:54PM (#17570673)
Java, PHP, C#, heck, even JavaScript/ES[6,7]. I feel like Python has a lot of its own ways of doing things, whereas a more idiomatic approach is imho a better way to go. Why the hell in the world would you call a class constructor as __init__? Pretty much every popular OOP language does it either by naming a constructor the same name as the class name or through some form of a `construct` notation. Why `self` and not `this`? I mean, I know that the language has a very low entry barrier, but wouldn't it make sense to design it in a more traditional way?

    if __name__ == '__main__':
        main()
Code like that just doesn't make any sense to me.
Re:Python has brought computer programming to a vast new aud... by duckerude on Thursday July 19, 2018 @06:19PM (#17570842)
Java, C# and modern PHP have a very similar style that's not at all universal. If you're used to those, Python is odd, but it can get a lot weirder. At least Python has classes.

__init__ is called that because it initializes the object. It could have been called "__construct__", but I think "__init__" is a lot clearer about what it actually does - the object is already created when it enters __init__, __init__ just prepares it.

Calling it "self" instead of "this" is super traditional. Smalltalk did it.

The __name__ == '__main__' pattern is ugly, and I'm not a huge fan of it. You don't always need it, it's just a guard to let you tell whether a script is run directly or imported as a module. The key is that Python only has runtime. A function declaration or class declaration is a fancy variable assignment. The content of the file is executed sequentially.

Python is not especially weird, but it is different from what you're used to. Wrapping your head around it takes a while, like with all languages, but things start to click eventually.

It helps to experiment. The REPL is great for that. If you don't understand why something is the way it is, mess with it until you think you understand its behavior, then mess with it some more.

Re:Python has brought computer programming to a vast new aud... by danso on Thursday July 19, 2018 @06:12PM (#17570792)
> whereas a more idiomatic approach is imho a better way to go.

I don't know what you mean by "idiomatic approach". That phrase, I thought, refers to languages that are highly opinionated about using a specific style -- this philosophy is encoded in Python's philosophy:

https://www.python.org/dev/peps/pep-0020/

There should be one-- and preferably only one --obvious way to do it.

> Pretty much every popular OOP language does it either by naming a constructor the same name as the class name or through some form of a `construct` notation

Seems kind of arbitrary to complain about the constructor keyword `__init__` when the constructor word for pre-ES6 JS, C, Perl, Ruby varies between prototype, constructor, __construct(), and new. Not sure how you say Python's use of `__init__` is somehow worse. As for why `self`, no idea, but seems to be a pretty easy convention to figure out.

As for the complaint about the `if __name__ == '__main__':`, to which language are you referring to that has a more self-evident way of encapsulating that functionality?

Re:Python has brought computer programming to a vast new aud... by andreygrehov on Thursday July 19, 2018 @07:23PM (#17571229)
I'm not talking about highly opinionated languages, but common idioms across OOP languages.

C doesn't have classes. pre-ES6 - agreed, but who writes it? I think Perl is a bad example, it's dying and is one of those highly opinionated guys, with all its `method`, `trusts`, etc.

> As for the complaint about the `if __name__ == '__main__':`, to which language are you referring to that has a more self-evident way of encapsulating that functionality?

My gut tells me that this is an error-prone idiom.

I'm not saying Python is bad, I just don't like it. I'd love to know what are the strongest sides of the language though?