CS190/295 Programming in Python for Life Sciences: Lecture 7 Instructor: Xiaohui Xie University of California, Irvine.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Python Objects and Classes
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Classes & Objects Computer Science I Last updated 9/30/10.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Object Oriented Programming in Python Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000.
C++ fundamentals.
Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 11 Introduction to Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 11.1.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Object Oriented Programing (OOP)
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Introduction to Object-Oriented Programming Lesson 2.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Introduction to OOP CPS235: Introduction.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Classes, Interfaces and Packages
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object-Oriented Programming (OOP) in Python References: Chapter 8.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Object Oriented Programming
COMPSCI 107 Computer Science Fundamentals
Computer Science 111 Fundamentals of Programming I
Chapter 4: Writing Classes
Inheritance Basics Programming with Inheritance
Chapter 6 Methods: A Deeper Look
Chapter 4 Writing Classes.
Computer Programming with JAVA
CS190/295 Programming in Python for Life Sciences: Lecture 7
Defining Classes and Methods
Computer Science 111 Fundamentals of Programming I
5. Functions Rocky K. C. Chang 30 October 2018
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Object-Oriented Programming
Presentation transcript:

CS190/295 Programming in Python for Life Sciences: Lecture 7 Instructor: Xiaohui Xie University of California, Irvine

Classes and Object Oriented Programming (OOP)

Introduction We've seen Python useful for –Simple scripts –Module design This lecture discusses Object Oriented Programming –Better program design –Better modularization

What is an object? An object is an active data type that knows stuff and can do stuff. More precisely, an object consists of: 1.A collection of related information. 2.A set of operations to manipulate that information.

Objects The information is stored inside the object in instance variables. The operations, called methods, are functions that “ live ” inside the object. Collectively, the instance variables and methods are called the attributes of an object.

Example: Multi-Sided Dice A normal die is a cube with six faces, each with a number from one to six. Some games use special dice with a different number of sides. Let ’ s design a generic class MSDie to model multi-sided dice.

Example: Multi-Sided Dice Each MSDie object will know two things: –How many sides it has. –It ’ s current value When a new MSDie is created, we specify n, the number of sides it will have.

Example: Multi-Sided Dice We have three methods that we can use to operate on the die: –roll – set the die to a random value between 1 and n, inclusive. –setValue – set the die to a specific value (i.e. cheat) –getValue – see what the current value is.

Example: Multi-Sided Dice >>> die1 = MSDie(6) >>> die1.getValue() 1 >>> die1.roll() >>> die1.getValue() 5 >>> die2 = MSDie(13) >>> die2.getValue() 1 >>> die2.roll() >>> die2.getValue() 9 >>> die2.setValue(8) >>> die2.getValue() 8

Example: Multi-Sided Dice Using our object-oriented vocabulary, we create a die by invoking the MSDie constructor and providing the number of sides as a parameter. Our die objects will keep track of this number internally as an instance variable. Another instance variable is used to keep the current value of the die. We initially set the value of the die to be 1 because that value is valid for any die. That value can be changed by the roll and setRoll methods, and returned by the getValue method.

Example: Multi-Sided Dice # msdie.py # Class definition for an n-sided die. import random class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = random.randrange(1, self.sides+1) def getValue(self): return self.value def setValue(self, value): self.value = value

Example: Multi-Sided Dice Class definitions have the form class : Methods look a lot like functions! Placing the function inside a class makes it a method of the class, rather than a stand-alone function. The first parameter of a method is always named self, which is a reference to the object on which the method is acting.

Example: Multi-Sided Dice Suppose we have a main function that executes die1.setValue(8). Just as in function calls, Python executes the following four-step sequence: –main suspends at the point of the method application. Python locates the appropriate method definition inside the class of the object to which the method is being applied. Here, control is transferred to the setValue method in the MSDie class, since die1 is an instance of MSDie.

Example: Multi-Sided Dice –The formal parameters of the method get assigned the values supplied by the actual parameters of the call. In the case of a method call, the first formal parameter refers to the object: self = die1 value = 8 –The body of the method is executed.

Example: Multi-Sided Dice –Control returns to the point just after where the method was called. In this case, it is immediately following die1.setValue(8). Methods are called with one parameter, but the method definition itself includes the self parameter as well as the actual parameter.

Example: Multi-Sided Dice The self parameter is a bookkeeping detail. We can refer to the first formal parameter as the self parameter and other parameters as normal parameters. So, we could say setValue uses one normal parameter.

Object Oriented Design (OOD) Object Oriented Design focuses on –Encapsulation: dividing the code into a public interface, and a private implementation of that interface –Polymorphism: the ability to overload standard operators so that they have appropriate behavior based on their context –Inheritance: the ability to create subclasses that contain specializations of their parents

Example: Atom class class atom: def __init__(self,symbol,x,y,z): self.symbol = symbol self.position = (x,y,z) def getsym(self): # a class method return self.symbol def __repr__(self): # overloads printing return '%s %10.4f %10.4f %10.4f' % (self.getsym(), self.position[0], self.position[1],self.position[2]) >>> at = atom(‘C’,0.0,1.0,2.0) >>> print at ‘C’ >>> at.getsym() 'C'

Atom class Overloaded the default constructor Defined class variables (symbol, position) that are persistent and local to the atom object Good way to manage shared memory: –instead of passing long lists of arguments, encapsulate some of this data into an object, and pass the object. –much cleaner programs result Overloaded the print operator We now want to use the atom class to build molecules...

Molecule Class class molecule: def __init__(self,name='Generic'): self.name = name self.atomlist = [] def addatom(self,atom): self.atomlist.append(atom) def __repr__(self): str = 'This is a molecule named %s\n' % self.name str = str+'It has %d atoms\n' % len(self.atomlist) for atom in self.atomlist: str = str + `atom` + '\n' return str

Using Molecule Class >>> mol = molecule('Water') >>> at = atom(‘O’,0.,0.,0.) >>> mol.addatom(at) >>> mol.addatom(atom(‘H’,0.,0.,1.)) >>> mol.addatom(atom(‘H’,0.,1.,0.)) >>> print mol This is a molecule named Water It has 3 atoms O H H Note that the print function calls the atoms print function –Code reuse: only have to type the code that prints an atom once; this means that if you change the atom specification, you only have one place to update.

Inheritance class organic_molecule(molecule): def countCarbon(self): n = 0 for atom in self.atomlist: if atom.getsym()==‘C’ : n=n+1 __init__, __repr__, and __addatom__ are taken from the parent class (molecule) Added a new function countCarbon() to count the number of carbons Another example of code reuse –Basic functions don't have to be retyped, just inherited –Less to rewrite when specifications change

Public and Private Data Currently everything in atom/molecule is public, thus we could do something really stupid like >>> at = atom(‘C’,0.,0.,0.) >>> at.position = 'Grape Jelly' that would break any function that used at.poisition We therefore need to protect the at.position and provide accessors to this data –Encapsulation or Data Hiding –accessors are "gettors" and "settors" Encapsulation is particularly important when other people use your class

Public and Private Data, Cont. In Python anything with two leading underscores is private __a, __my_variable Anything with one leading underscore is semi-private, and you should feel guilty accessing this data directly. _b –Sometimes useful as an intermediate step to making data private

Encapsulated Atom class atom: def __init__(self,symbol,x,y,z): self.symbol = symbol self.__position = (x,y,z) #position is private def getposition(self): return self.__position def setposition(self,x,y,z): self.__position = (x,y,z) #typecheck first! def translate(self,x,y,z): x0,y0,z0 = self.__position self.__position = (x0+x,y0+y,z0+z)

Why Encapsulate? By defining a specific interface you can keep other modules from doing anything incorrect to your data By limiting the functions you are going to support, you leave yourself free to change the internal data without messing up your users –Write to the Interface, not the the Implementation –Makes code more modular, since you can change large parts of your classes without affecting other parts of the program, so long as they only use your public functions

Classes that look like arrays Overload __getitem__(self,index) to make a class act like an array class molecule: def __getitem__(self,index): return self.atomlist[index] >>> mol = molecule('Water') #defined as before >>> for atom in mol: #use like a list! print atom >>> mol[0].translate(1.,1.,1.) Previous lectures defined molecules to be arrays of atoms. This allows us to use the same routines, but using the molecule class instead of the old arrays. An example of focusing on the interface!

Classes that look like functions Overload __call__(self,arg) to make a class behave like a function class gaussian: def __init__(self,exponent): self.exponent = exponent def __call__(self,arg): return math.exp(-self.exponent*arg*arg) >>> func = gaussian(1.) >>> func(3.)

Other things to overload __setitem__(self,index,value) –Another function for making a class look like an array/dictionary –a[index] = value __add__(self,other) –Overload the "+" operator –molecule = molecule + atom __mul__(self,number) –Overload the "*" operator –zeros = 3*[0]

Other things to overload, cont. __len__(self) –Overload the len() command –natoms = len(mol) __getslice__(self,low,high) –Overload slicing –glycine = protein[0:9] __cmp__(self,other): –On comparisons (<, ==, etc.) returns -1, 0, or 1, like C's strcmp

Acknowledgement Some of slides are from Richard Muller and John Zelle