Know Java, and know Python is not Java These slides compare the two, assuming Java knowledge for fair comparison. Don’t try to write Python like Java, though.
public class MyClass { public void display() { System.out.println("Hello, world!"); } } public class MySubClass extends MyClass { public void display() { System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java
public class MySubClass extends MyClass { public void display() { super.display(); System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java
public interface Animal { public void eat(); public void travel(); } public class Mammal implements Animal { public void eat() { System.out.println("Mammal eats"); ... Animal.java, Mammal.java: Interfaces in Java
class Boat(object): """Model a boat.""" length = 0 color = None def __init__(self, length, color): ... def __gt__(self, other): return self.length > other.length: boat.py: Using the data model
__lt__ - less than __le__ - less than or equal __eq__ - equal __ne__ - not equal __gt__ - greater than __ge__ - greater than or equal __cmp__ - fallback, if above not implemented boat.py: Using the data model
class Person(object): weight_kg = 0 @property def weight_lb(self): return self.weight_kg * 2.2 @weight_lb.setter def weight_lb(self, value): self.weight_kg = value / 2.2 boat.py: Using the data model