CS 200 - Object Oriented Programming


[Home]

Principles

  • Encapsulation. Python uses namespaces to achieve encapsulation. Other OOP languages have private and public data and methods. Python, not so much.

  • Polymorphism. Many shapes. The same method can mean different things depending on the object on which it is invoked. Consider the install method. Means different things for a refridgerator, stove, television, alarm system, IKEA cabinet, or military dictatorship.


    What is depicted above?

  • Inheritance. Uses ISA hierarchy principle from cognitive psychology.

    Terminology

    Class:
    A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
    class course: 
    

    Class variable:
    A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.
    count = 0
    classes = []
    

    Data member:
    A class variable or instance variable that holds data associated with a class and its objects.
    count = 0
    self.count = self.__class__.count
    or
    self.count = course.count
    

    Function overloading:
    The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.
    
    def __init__(self, title, dept):
    
    

    Instance variable:
    A variable that is defined inside a method and belongs only to the current instance of a class.
    self.title = title
    

    Inheritance:
    The transfer of the characteristics of a class to other classes that are derived from it.
    class gradcourse(course):
    
        def __init__(self, title, dept):
            course.__init__(self, title, dept)
            self.grad = True
    

    Instance:
    An individual object of a certain class. An object obj that belongs to a class course, for example, is an instance of the class course.
    c = course("CS200", "Computer Science")
    

    Instantiation:
    The creation of an instance of a class.
    c = course("CS200", "Computer Science")
    

    Method:
    A special kind of function that is defined in a class definition.
    def add_student(self, item):
    

    Object:
    A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.
    c = course("CS200", "Computer Science")
    

    Operator overloading:
    The assignment of more than one function to a particular operator.
    a = 1 + 2
    b = "hello " + "world"
    

    Examples

    oop.py

    The Big Picture

    (from Learning Python, 5th Edition, page 1077.)

    The most common reasons to use Object Oriented Programming:

    Code reuse
    This one's easy (and is the main reason for using OOP). By supporting inheritance, classes allow you to program by customization instead of starting each program from scratch.

    Encapsulation
    Wrapping up implementation details behind object oriented interfaces insulates users of a class from code changes.

    Structure
    Classes provide new local scopes, which minimizes name clashes. They also provide a natural place to write and look for implementation code, and to manage object state.

    Maintenance
    Classes naturally promote code factoring, which allows us to minimize redundancy. Thanks both to the structure and code reuse support of classes, usually only one copy of the code needs to be changed.

    Consistency
    Classes and inheritance allow you to implement common interfaces, and hence create a common look and feel in your code; this eases debugging, comprehension, and maintenance.

    Polymorphism
    This is more a property of OOP than a reason for using it, but by supporting code generality, polymorphism makes code more flexible and widely applicable, and hence more reusable.

    Other
    And, of course, the number one reason students gave for using OOP: it looks good on a resume! (OK, I threw this one in as a joke, but it is important to be familiar with OOP if you plan to work in the software field today.)

    Finally, ... you won't fully appreciate OOP until you've used it for a while. Pick a project, study larger examples, work through exercises -- do whatever it takes to get your feet wet with OO code; it's worth the effort.


    [Home]