开发者

overview of differences between inheritance in python and java

I have a background in java, and I'm learning python. I'll need to be using inheritance soon, and I want to find out what any key differences are between how things are done in each of them. I've had a look online and found some information about di开发者_JAVA百科fferences between the two and what to look out for, and I've found that python supports multiple inheritance, but I'd like to know about any other differences specific to inheritance. I don't need to be told syntax (unless there is something I really need to watch out for), I've already taken a look at it and I'll be fine with that.

Python is not Java

Python classes: Multiple Inheritance

Python for Java programmers

I can't really find exactly what I'm looking for which is an overview of differences and what to watch out for.


Java has a fairly straightforward inheritance model: classes must have one (and only one) parent . You can't inherit from multiple parents, although you can implement multiple interfaces, which can be seen as Java's version of "multiple inheritance."

Most methods in Java classes are dynamically (late) bound, with the exception of methods declared static, private and final in the parent class.

In Python, as you have noted, you can inherit from multiple (or no) parents. Note that with multiple inheritance you can get the "diamond problem." You should be aware of how Python resolves that and the implications this has when you refer to a parent class in Python (ie: who's your daddy?)

In Python, everything is dynamically bound, and since you can add members to an instance, not all instances of the same class are guaranteed to have the same members.

Finally, there's a slight difference in how constructors are overridden: In Java, children classes must call the parent's constructor (refinement overriding), whereas in Python children classes can override the constructor and not call the parent's constructor (replacement overriding).


The most important concept to grasp when coming from a java background is that class inheritance is tool that is seldom required to solve problems in python. The buzzword for this idea is duck typing.

Most of a python program is accessing and invoking the attributes on on object or another. As in java, you can get useful behaviour by giving a function that expects an object of one type an object of another, more refined type. For this to work in java, it's neccesary that the substituted object either inherit from the expected type, or implement the expected interface.

In python, that's not necessary at all; If the substituted object has all of the attributes the function you're passing it to expects, then it will just work.

The only reason to reach for inheritance in python is because the superclass really, genuinely does almost all of the things you need the new type to do, and you only have to add a few extra behaviours.

The most abundant example of this is the iterator protocol in python. Any object that has a method __iter__() that returns the object itself and a next() method that returns anything at all is an iterator, and can appear a for statement. str, list, dict, file and many other types, which have nothing much in common, and only the global object as common superclass, each implement the iterator protocol.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜