Object Oriented Python - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Object Oriented Python

Description:

Uses software objects - discrete, reusable units of code ... Uninitialized attributes will return an AttributeError exceptio if they are referenced ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 15
Provided by: rachelk1
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Python


1
Object Oriented Python
2
Object Oriented Programming
  • Programming paradigm
  • Uses software objects - discrete, reusable units
    of code
  • Different from modular programming which focuses
    on the function of a module in that it also
    incorporates data
  • Allows abstraction, polymorphism (same method
    does different things for objects of different
    classes), etc.

3
Software Objects
  • Objects have attributes (data) and behaviors
    (methods).
  • A class is a larger category of object
  • Class hierarchy
  • Ex. Parent class Animal
  • Subclass of animal Dog
  • Subclass of Dog Chihuahua, Lab, Collie

4
Dog Object
  • Attributes (Data)
  • Size
  • Fur color
  • Breed
  • Behavior (Methods)
  • Bark
  • Sleep
  • Eat
  • Fetch

5
Python Classes
  • class dog
  • abstraction of a dog
  • def __init__(self, size, fur_color, breed)
  • self.size size
  • self.color fur_color
  • self.breed breed
  • def bark(self)
  • code to execute
  • def bark_twice(self)
  • self.bark()
  • self.bark()

6
self, init
  • __init__ is called immediately after the class is
    instantiated
  • __init__ never returns a value
  • The first argument of every method in a class is
    a reference to the instantiation of the class
    that is calling the method represented by self
  • def methodname(self, arg1, arg2, arg3, )
  • Do not use self when calling the method using the
    instantiation e.g. instance.methodname(arg1,arg2,
    arg3)

7
Inheritance
  • class mammal
  • abstraction of a mammal
  • def __init__(self, )
  • class dog(mammal)
  • abstraction of a dog
  • def __init__(self, size, fur_color, breed)
  • mammal.__init__(self)
  • Multiple Inheritance
  • class dog(vertebrate, mammal)

8
Instantiation
  • import dog
  • lassie dog(medium,brown,collie)
  • lassie.bark()
  • lassie.save_timmy()
  • size lassie.size
  • Do not use self when instantiating an object,
    retrieving an attribute or calling a method.

9
Special Methods
  • Convert non-method-calling syntax into a method
    call
  • Ex. With a dictionary dictkeyname uses
    __getitem__
  • __setitem__
  • __getitem__
  • __repr__ - return string representation of object
  • __cmp__ - called when you use
  • __len__ - called when you use lenI()
  • __delitem__ - called when you use del to delete
    an item from a dictionary

10
Private Functions/Methods/Attributes
  • Cannot be called outside of their modules/classes
  • Determined by name
  • Must start with 2 underscores but not end with 2
    underscores __private or __private_
  • Calling private methods will raise an exception

11
Good Practice
  • Include docstrings in your class
  • Initialize all attribute variables to something
    reasonable in the __init__ section of the class
  • Uninitialized attributes will return an
    AttributeError exceptio if they are referenced

12
Notes
  • Python does not support function overloading
  • Methods are defined solely by their name and
    cannot be distinguished by the number of input
    parameters they expect
  • The __init__ statement of the subclass always
    overrides that of the parent class

13
More Info
  • Dive into Python

14
Exercises
  • Look over matrix.py on the course website
  • Adapt this code to create a matrix class capable
    of handling 3D matrices as well as 2D
  • Check out http//docs.python.org/tutorial/errors.h
    tml - look at the user defined exceptions section
  • Use this information to design your own exception
    class. Have your class inherit from the existing
    python Exception class
Write a Comment
User Comments (0)
About PowerShow.com