News for hackers. Stuff that matters.
Re:Python has brought computer programming to a vast new aud... by pm90 on Thursday July 19, 2018 @05:04PM (#17570246)
Python is a great language to start programming in. My biggest frustration with traditional programming languages (well, C/Java mostly) was just how much upfront effort and understand is required to do useful stuff. Whereas with python it is pretty easy to whip up a script, read input and transform it into output in real time etc.

That said, I think its also important for Python programmers to dabble at least a little bit with Java to really understand how OOP works. Python does have OOP but it has certain quirks which can confuse newcomers. Java helped to clarify my understanding of OOP a lot, even though I've only used it marginally.

Re:Python has brought computer programming to a vast new aud... by nishantvyas on Thursday July 19, 2018 @05:11PM (#17570308)
W/O going into the war of I'm better than you (Python vs JAVA)... Python is built to make it easy to write good programs... where as JAVA is built to make it hard to write bad programs.... same objective but different thinking/approach to achieve it... hence one(Python) would eventually have larger adoption....
Re:Python has brought computer programming to a vast new aud... by kickopotomus on Thursday July 19, 2018 @06:17PM (#17570829)
I agree with this sentiment but it is a bit of a double edged sword for Python professionally. There is some really good Python code out there but there is also a lot of bad stuff too. I know this can be said for every language but I think I have seen it most in languages like JS and Python because of the ease of access.
Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @06:26PM (#17570884)
A better measure would be the amount of bad code produced conditioned on the skill/experience of the author. I'd be more worried about a beginner C programmer than a beginner Python programmer.
Re:Python has brought computer programming to a vast new aud... by eikenberry on Thursday July 19, 2018 @07:04PM (#17571128)
I don't think Java's take on OOP is good to learn before you really understand OOP already. Its heavy handed style of OOP really hasn't aged that well and is rightfully being avoided in all newer languages.
Re:Python has brought computer programming to a vast new aud... by kjeetgill on Thursday July 19, 2018 @09:25PM (#17571773)
I couldn't agree more. I don't know how to explain it exactly, but I had the opposite experience. Learning basics of functional programming in Python improved my OOP in Java over time.

I think it's probably more like this: learning a second, contrasting topic really helps explore the motivations that drive their respective goals.

OOP and Functional are broad classes of contrasting design patterns and philosophies but share the common idea: be principled about state.

Re:Python has brought computer programming to a vast new aud... by oh-kumudo on Friday July 20, 2018 @05:30AM (#17573604)
There is not an Universal OOP specification to follow.

In Java's case, a lot OOP flavor's design patterns are created due to Java's lacking of some primitives like closure/lambda and higher-order functions back in its old days.

In short, Java's OOP solves many problems that are unique to Java and could be easily avoided in other languages.

Re:Python has brought computer programming to a vast new aud... by kjeetgill on Thursday July 19, 2018 @09:11PM (#17571727)
I'm going to chime in to 100% agree with you. I'm a big fan of both of those languages and their respective ecosystems.

I'm in love with the idea that there a good case of polyglot (biglot?) program synergy between the two. Keep your eyes open on Jython and GraalPython!

Re:Python has brought computer programming to a vast new aud... by meowface on Thursday July 19, 2018 @05:10PM (#17570301)
Other than interfaces and generics (which would make no sense to add in Python, since it's dynamically typed), can you name some OOP features Java has that Python doesn't have?
Re:Python has brought computer programming to a vast new aud... by pm90 on Thursday July 19, 2018 @05:34PM (#17570484)
This may sound super n00b but I really like that Java is explicit about everything. So for instance, you explicitly specify variables, methods as private with the keyword 'private'. In python you have to use an underscore, and its still not really private, its obfuscated. That's just one example, but in general I really like that kind of by-the-book verbosity, instead of having to mentally map it into the OOP concepts.
Re:Python has brought computer programming to a vast new aud... by ben-schaaf on Thursday July 19, 2018 @06:53PM (#17571053)
Double underscore is the real 'private':

    >>> class Foo:
            def __fn(self):
                return 2
    >>> Foo().__fn()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Foo' object has no attribute '__fn'
    >>> Foo()._Foo__fn()
    2
A single underscore is more like 'protected', ie. it doesn't prevent subclasses from easily/accidentally overriding your function.
Re:Python has brought computer programming to a vast new aud... by kjeetgill on Thursday July 19, 2018 @09:29PM (#17571790)
I'm less concerned with encapsulation, but the explicitness is a godsend the larger your codebase or team.

Python keeps code much smaller but above a certain size I wish I could "promote" parts of my code to Java.

Partial typing in Python just isn't close to feeling the same.

Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @06:24PM (#17570864)
Why would you want to actually enforce privacy? That makes it harder for the user to patch. Better to provide a mild discouragement via "_name".
Re:Python has brought computer programming to a vast new aud... by twblalock on Thursday July 19, 2018 @06:35PM (#17570945)
Because in traditional object-oriented design you would extend rather than patch. It's the open-closed principle.

There are many benefits to enforcing privacy, and one of the biggest ones is that you can make changes to the internals of your library without breaking your users' code, because they never had access to the internals.

Re:Python has brought computer programming to a vast new aud... by xapata on Thursday July 19, 2018 @06:42PM (#17570985)
You can get that benefit with Python. Name mangling helps. It gets the benefits of `final` without the annoying enforcement.

Don't stop me from patching your buggy code. If writing an extension is more efficient, that's how I'd solve the problem.

Re:Python has brought computer programming to a vast new aud... by marvin on Thursday July 19, 2018 @07:03PM (#17571118)
That's an eternal ideological debate where there is always a rebuttal on the form "a-ha - but you could just...", but the rationale is this: Your class will probably be doing things with the private variables that aren't obvious, and will break things if you access them from outside. (Either by them having a different value than you expect, or expecting a different value than you set).

Of course, if you have a perfect understanding of the code, this will never be a problem, as the program is always executed as it is written. But most likely, not everyone in your organization will be as patient, smart and attentive to the details as you are.

Re:Python has brought computer programming to a vast new aud... by StavrosK on Thursday July 19, 2018 @07:51PM (#17571373)
I don't understand the argument, though. Python's underscore variables are meant to not be accessed from outside. The argument against them isn't that you might be doing some non-obvious things inside the class, that's the whole point of them!

It's a way for the class user to say "I know this is terrible, I know this might break, I want to do it anyway".

Re:Python has brought computer programming to a vast new aud... by ychen306 on Thursday July 19, 2018 @09:36PM (#17571802)
The obvious usage of privacy is so you don't expose an API you don't want to maintain. It's also occasionally used to maintain some invariants; for instance, you can have a public method that wraps around some protected interface that does input checking/clean up. Having the compiler enforcing privacy, in this case, guarantees that whatever actual method you dynamically dispatches to has the "right" input (accidentally calling the interface directly won't compile).
Re:Python has brought computer programming to a vast new aud... by chomp on Thursday July 19, 2018 @06:41PM (#17570974)
Private doesn't mean much. You can still access private members in Java using reflection.
Re:Python has brought computer programming to a vast new aud... by kjeetgill on Thursday July 19, 2018 @09:09PM (#17571709)
This hole is incrementally being closed with the introduction of Java 9's modle system. You can only deep reflect into classes in your modules or those "open" to you.

There are both declaritive and command line param ways to open things so it's not as rigid as it sounds.

Re:Python has brought computer programming to a vast new aud... by kimdotcom on Thursday July 19, 2018 @09:28PM (#17571787)
Check out Ada.
Re:Python has brought computer programming to a vast new aud... by Alex3917 on Thursday July 19, 2018 @05:18PM (#17570377)
> can you name some OOP features Java has that Python doesn't have?

Encapsulation. In Python you can write code in an OO style and benefit from inheritance, but it’s really only good for people writing libraries and frameworks. For end user code, objects are best avoided because they can pick up state in unexpected ways, which leads to problems that are extremely difficult to debug.

Re:Python has brought computer programming to a vast new aud... by meowface on Thursday July 19, 2018 @07:23PM (#17571230)
Could you give some examples? What encapsulation features does Java have that Python doesn't? You can get private methods with double underscores. It's true that attributes in Python objects are all public by default and in Java you have to be more explicit to get public attributes, so maybe that counts, but otherwise what do you mean by "objects can pick up state in unexpected ways"?
Re:Python has brought computer programming to a vast new aud... by Alex3917 on Thursday July 19, 2018 @08:01PM (#17571424)
So let's so you have:

  class Foo():
      def __init__(self):
          self.bar = 1
Then let's say you have foo_instance imported in your module, and you see that foo_instance.bar has a value of 2.

You can see that foo_instance is imported from module A. So you go there, and see that the instance wasn't created there, it was imported from module B. But wait, it wasn't created there either, it was imported from module C, etc.

Somewhere along the way bar picked up the value of 2. But it's almost impossible to figure out where because there isn't any requirement use a uniquely-named accessor to modify the instance, so you can't just grep through the code to see where that method was called. (And maybe the name of the instance has changed a few times along the way, for whatever reason.)

And what's worse, the code that modified the instance might not even be visible in any of the modules, it's possible that someone took advantage of Python's ability to change the way the importing system works to mess with either the class or the instance. Or maybe there is some global middleware that's messing with objects or instances, or unrelated module Z is secretly monkey patching module B somewhere in the middle of this process. Who knows, and good luck figuring out.

Whereas if you just write the user-code logic in functions then it's not possible for developers to create situations like this. That obviously isn't possible for a library where the entire point is to make everything extensible and where you don't know how people will want to use something in advance, but hopefully the people writing libraries and frameworks have good enough judgment not to completely abuse the tools.

I'm not sure if I can really articulate how this is different than Java or Swift or whatever, but for whatever reason it just feels different.

Re:Python has brought computer programming to a vast new aud... by meowface on Thursday July 19, 2018 @08:33PM (#17571548)
I understand your point. I think this is partly related to the fact that instance attributes are public by default and do not require accessor or mutator methods (though you can easily create them with the @property decorator) and partly due to the fact that Python is a very dynamic language.

You can make instance variables "culturally private" by pretending the variable name with a single underscore, and you can name-mangle (to make it effectively private, unless someone really goes out of their way) with double underscores, but I know that's annoying to do for most/all variables and that making them private by default has advantages.

Python is just not as strict a language as Java, Swift, C#, etc. That has advantages and disadvantages.

Personally, this hasn't generally been an issue for me, because I've rarely directly modified instance variables in my own code unless I'm intentionally doing something really "meta", and it's not a pattern I've seen much of in other codebases.