Slide 1

Slide 1 text

Stephen Finucane Software Developer, OpenStack Team Intel Shannon

Slide 2

Slide 2 text

What is Object Orientated design, and why do we care? What is an object?

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

class MyClass(object): def __init__(self, *args, **kwargs): pass def do_stuff(self): print('doing stuff') >> instance = MyClass() >> instance.do_stuff() doing stuff classes.py: An example of classes in Python

Slide 6

Slide 6 text

class MyClass(object): def __init__(self, *args, **kwargs): pass def do_stuff(self): print('doing stuff')

Slide 7

Slide 7 text

object is Python being explicit You don’t need it in Python 2, but you should use it to get “new style classes”

Slide 8

Slide 8 text

self is also Python being explicit Think of it like the ‘this’ keyword in Java

Slide 9

Slide 9 text

public class MyClass { String name; ... public void setName(String name) { this.name = name; } } MyClass.java: The ‘this’ keyword

Slide 10

Slide 10 text

class MyClass(object): name = None ... def set_name(self, name): self.name = name self._example.py: The ‘self’ keyword

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

public class MyClass { public MyClass(String name) { ... } public MyClass(String name, int age) { ... } } MyClass.java: Constructors in Java

Slide 13

Slide 13 text

class MyClass(object): def __init__(name, age=None): ... constructors.py: Constructors in Python

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

class MyClass(object) { def display(self): print('Hello world') class MySubClass(MyClass): def display(self): print('Testing 123') constructors.py: Constructors in Python

Slide 16

Slide 16 text

public class MySubClass extends MyClass { public void display() { super.display(); System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java

Slide 17

Slide 17 text

class MySubClass(MyClass): def display(self): super(MySubClass, self).display(self) print('Testing 123') superclasses.py: Accessing the Super Class

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

class Mammal: def eat(self): print('Mammal eats') ... class Reptile: def eat(self): print('Reptile eats') ... animals.py: Duck Typing in Python

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

>> stephens_boat = Boat(...) >> ankurs_boat = Boat(...) >> stephens_boat > ankurs_boat True The data model in practice

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

__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

Slide 25

Slide 25 text

>>> stephens_boat = Boat(100, 'red') >>> stephens_boat.__doc__ Model a boat. >>> stephens_boat.__dict__ {'color': 'red', 'length': 100} >>> stephens_boat.__class__ The data model in practice

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

• https://docs.python.org/2/tutorial/classes.html • https://en.wikipedia.org/wiki/Duck_typing • https://stackoverflow.com/questions/222877/how-to-use-super- in-python • https://docs.python.org/2/reference/datamodel.html