Python Object

Python Object

How objects work in Python. A talk I gave to OSIC members, October 2015.

8fbd28ad59a1aa317a5ec175b0778359?s=128

Stephen Finucane

July 12, 2016
Tweet

Transcript

  1. Stephen Finucane Software Developer, OpenStack Team Intel Shannon

  2. What is Object Orientated design, and why do we care?

    What is an object?
  3. 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.
  4. None
  5. 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
  6. class MyClass(object): def __init__(self, *args, **kwargs): pass def do_stuff(self): print('doing

    stuff')
  7. object is Python being explicit You don’t need it in

    Python 2, but you should use it to get “new style classes”
  8. self is also Python being explicit Think of it like

    the ‘this’ keyword in Java
  9. public class MyClass { String name; ... public void setName(String

    name) { this.name = name; } } MyClass.java: The ‘this’ keyword
  10. class MyClass(object): name = None ... def set_name(self, name): self.name

    = name self._example.py: The ‘self’ keyword
  11. None
  12. public class MyClass { public MyClass(String name) { ... }

    public MyClass(String name, int age) { ... } } MyClass.java: Constructors in Java
  13. class MyClass(object): def __init__(name, age=None): ... constructors.py: Constructors in Python

  14. 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
  15. class MyClass(object) { def display(self): print('Hello world') class MySubClass(MyClass): def

    display(self): print('Testing 123') constructors.py: Constructors in Python
  16. public class MySubClass extends MyClass { public void display() {

    super.display(); System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java
  17. class MySubClass(MyClass): def display(self): super(MySubClass, self).display(self) print('Testing 123') superclasses.py: Accessing

    the Super Class
  18. None
  19. 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
  20. class Mammal: def eat(self): print('Mammal eats') ... class Reptile: def

    eat(self): print('Reptile eats') ... animals.py: Duck Typing in Python
  21. None
  22. >> stephens_boat = Boat(...) >> ankurs_boat = Boat(...) >> stephens_boat

    > ankurs_boat True The data model in practice
  23. 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
  24. __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
  25. >>> stephens_boat = Boat(100, 'red') >>> stephens_boat.__doc__ Model a boat.

    >>> stephens_boat.__dict__ {'color': 'red', 'length': 100} >>> stephens_boat.__class__ <class '__main__.Boat'> The data model in practice
  26. None
  27. 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
  28. None
  29. • 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