Object Oriented Programming in Python Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

Python Objects and Classes
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Object-Oriented PHP (1)
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.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Object-oriented Programming Concepts
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
ASP.NET Programming with C# and SQL Server First Edition
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Introduction to Object-oriented programming and software development Lecture 1.
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.
Object Oriented Design and Programming Alan Goude Room: Sheaf 9323.
Introduction to Python III CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
Chapter 11 Introduction to Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 11.1.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Programming in Java CSCI-2220 Object Oriented Programming.
CS190/295 Programming in Python for Life Sciences: Lecture 7 Instructor: Xiaohui Xie University of California, Irvine.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
 Computer Science 1MD3 Introduction to Programming Michael Liut Ming Quan Fu Brandon.
Object Oriented Programing (OOP)
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
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.
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.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
1 Introduction to Object Oriented Programming Chapter 10.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Modern Programming Tools And Techniques-I
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Object Oriented Programming in Python: Defining Classes
COMPSCI 107 Computer Science Fundamentals
MPCS – Advanced java Programming
Low Budget Productions, LLC
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
An Introduction to Inheritance
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Creating and Deleting Instances Access to Attributes and Methods
Object Oriented Analysis and Design
CS190/295 Programming in Python for Life Sciences: Lecture 7
Object-Oriented Programming
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Object-Oriented PHP (1)
Presentation transcript:

Object Oriented Programming in Python Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000

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

What is an Object? A software item that contains variables and methods 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

Namespaces At the simplest level, classes are simply namespaces class myfunctions: def exp(): return 0 >>> math.exp(1) >>> myfunctions.exp(1) 0 It can sometimes be useful to put groups of functions in their own namespace to differentiate these functions from other similarly named ones.

Python Classes Python contains classes that define objects –Objects are instances of classes class atom: def __init__(self,atno,x,y,z): self.atno = atno self.position = (x,y,z) __init__ is the default constructor self refers to the object itself, like this in Java.

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

Atom class Overloaded the default constructor Defined class variables (atno,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(8,0.,0.,0.) >>> mol.addatom(at) >>> mol.addatom(atom(1,0.,0.,1.)) >>> mol.addatom(atom(1,0.,1.,0.)) >>> print mol This is a molecule named Water It has 3 atoms 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 qm_molecule(molecule): def addbasis(self): self.basis = [] for atom in self.atomlist: self.basis = add_bf(atom,self.basis) __init__, __repr__, and __addatom__ are taken from the parent class (molecule) Added a new function addbasis() to add a basis set Another example of code reuse –Basic functions don't have to be retyped, just inherited –Less to rewrite when specifications change

Overloading parent functions class qm_molecule(molecule): def __repr__(self): str = 'QM Rules!\n' for atom in self.atomlist: str = str + `atom` + '\n' return str Now we only inherit __init__ and addatom from the parent We define a new version of __repr__ specially for QM

Adding to parent functions Sometimes you want to extend, rather than replace, the parent functions. class qm_molecule(molecule): def __init__(self,name="Generic",basis="6-31G**"): self.basis = basis molecule.__init__(self,name) call the constructor for the parent function add additional functionality to the constructor

Public and Private Data Currently everything in atom/molecule is public, thus we could do something really stupid like >>> at = atom(6,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,atno,x,y,z): self.atno = atno 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] __getattr__(self,name) –Overload attribute calls –We could have done atom.symbol() this way

Other things to overload, cont. __del__(self) –Overload the default destructor –del temp_atom __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

References Design Patterns: Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (The Gang of Four) (Addison Wesley, 1994) Refactoring: Improving the Design of Existing Code, Martin Fowler (Addison Wesley, 1999) Programming Python, Mark Lutz (ORA, 1996).