0
$\begingroup$

What would one suggest for a junior lever programming student to learn object oriented Python? For me, it is quite easy to understand objects in Java and non-object oriented Python but objects in Python seems weirder than in Java. Are there good books or lecture notes?

$\endgroup$
1
  • 5
    $\begingroup$ Weirder in what way? The object models are very similar. Python misses explicit interfaces, of course. The big difference is when a reference is checked for validity. $\endgroup$
    – Buffy
    Commented Oct 25, 2021 at 11:44

1 Answer 1

1
$\begingroup$

The object models of Java and Python are just about the same. But the reference model is a bit different. But the biggest difference is that in Java, a reference variable is defined to have a certain type (class) and in Python it is just a name. In other words, in Java, references are typed. In Python they are untyped. But in both languages the objects themselves are strongly typed at time of creation.

This difference implies that reference types (validity) are checked by the compiler - or even earlier when a good IDE is used. Thus Eclipse will highlight type errors in Java but cannot do so in Python. In Python, the validity of an expression is checked only when the program actually runs since any variable can actually refer to any object, regardless of type.

In Python if you say foo.bar(x); then this is valid or not depending on whether, at that moment the object that foo references has a method bar with appropriate parameters. The actual type of that object doesn't matter, only its available methods do. But the object itself, as distinct from the reference to it, has a specific type that doesn't change.

But in Java, this gets checked before the program runs, making error detection earlier in the process. If the declared class of foo, which is explicit in the program text has a matching bar method then the expression is guaranteed valid prior to the run.

For the programmer, this implies that you need to take more care in writing your program in Python since you get fewer hints from the IDE or the compiler if you don't use an IDE. Thus thorough testing becomes even more important in Python since untested code can harbor errors for a long time undetected.

I would hope that any decent undergraduate level programming text in either language will give you proper guidance and a mental model for that language.

$\endgroup$
4
  • $\begingroup$ The most obvious difference between Python and Java is multiple inheritance, but since Python uses MI with linearization, there is still a single, well-defined, linear chain of ancestors for each class, just like in Java. $\endgroup$ Commented Oct 29, 2021 at 13:44
  • $\begingroup$ @JörgWMittag, Java has multiple inheritance? Since when? Implementing multiple interfaces isn't the same thing. $\endgroup$
    – Buffy
    Commented Oct 29, 2021 at 13:56
  • $\begingroup$ Java doesn't, but Python does, hence it's (at least to me) the most obvious difference between Java and Python. $\endgroup$ Commented Oct 29, 2021 at 14:05
  • $\begingroup$ I say "Java is a Victorian convent and Python is a nudist colony," because Java has a much stronger form of encapsulation in the form of access specifiers. There is no compiler to enforce privacy in Python. This amounts to a very significant difference. Python has no notion of finality either. $\endgroup$ Commented Nov 20, 2021 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.