Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
ASP.NET Programming with C# and SQL Server First Edition
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Object-Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
Chapter 8 More Object Concepts
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Functions Chapter 4 Python for Informatics: Exploring Information
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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)
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Inheritance Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Object Oriented Programing (OOP)
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Object Oriented Programming In Python
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Python – May 19 Review –What is the difference between: list, tuple, set, dictionary? –When is it appropriate to use each? Creating our own data types:
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Python Aliasing Copyright © Software Carpentry 2010
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Chapter 7 Object-Oriented Programming
Program Design Invasion Percolation: Aliasing
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
GENERAL OOPs CONCEPTS.
OOP What is problem? Solution? OOP
Lecture VI Objects The OOP Concept Defining Classes Methods
Object Oriented Programming
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 22 Inheritance Richard Gesick.
CSc 110, Autumn 2016 Lecture 31: Encapsulation
Chapter 9 Objects and Classes
Week 8 Classes and Objects
Data Structures – 1D Lists
Object Oriented Programming in Python
Overview of OOP Terminology
Conrad Huang Genentech Hall, N453A x6-0415
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Classes and Objects

Basics Two basic concepts in OOP are class and object

Classes and ObjectsBasics Two basic concepts in OOP are class and object A class defines the behavior of a new kind of thing

Classes and ObjectsBasics Two basic concepts in OOP are class and object A class defines the behavior of a new kind of thing An object is a thing with particular properties

Classes and ObjectsBasics BiologyProgramming General Specific Two basic concepts in OOP are class and object A class defines the behavior of a new kind of thing An object is a thing with particular properties

Classes and ObjectsBasics BiologyProgramming General Species canis lupus Specific Organism Waya Two basic concepts in OOP are class and object A class defines the behavior of a new kind of thing An object is a thing with particular properties

Classes and ObjectsBasics BiologyProgramming General Species canis lupus Class Vector Specific Organism Waya Object velocity Two basic concepts in OOP are class and object A class defines the behavior of a new kind of thing An object is a thing with particular properties

Classes and ObjectsBasics Define a new class with no behavior

Classes and ObjectsBasics Define a new class with no behavior >>> class Empty(object):... pass

Classes and ObjectsBasics Define a new class with no behavior >>> class Empty(object):... pass Create two objects of that class

Classes and ObjectsBasics Define a new class with no behavior >>> class Empty(object):... pass Create two objects of that class >>> first = Empty() >>> second = Empty()

Classes and ObjectsBasics Define a new class with no behavior >>> class Empty(object):... pass Create two objects of that class >>> first = Empty() >>> second = Empty() >>> print 'first is', id(first) >>> print 'second is', id(second)

Classes and ObjectsBasics Contents of memory first second Empty

Classes and ObjectsBasics Contents of memory first second Empty object

Classes and ObjectsBasics Define the class's behavior with methods

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class…

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class class Greeter(object): def greet(self, name): print 'hello', name, '!'

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class class Greeter(object): def greet(self, name): print 'hello', name, '!'

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class class Greeter(object): def greet(self, name): print 'hello', name, '!'

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class class Greeter(object): def greet(self, name): print 'hello', name, '!' g = Greeter() g.greet('Waya') hello Waya !

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class class Greeter(object): def greet(self, name): print 'hello', name, '!' g = Greeter() g.greet('Waya') hello Waya !

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class class Greeter(object): def greet(self, name): print 'hello', name, '!' g = Greeter() g.greet('Waya') hello Waya !

Classes and ObjectsBasics Define the class's behavior with methods A function defined inside a class… …that is called for an object of that class class Greeter(object): def greet(self, name): print 'hello', name, '!' g = Greeter() g.greet('Waya') hello Waya !

Classes and ObjectsBasics Contents of memory greet stackheap g Greeter

Classes and ObjectsBasics Contents of memory greet stackheap g Greeter self name 'Waya'

Classes and ObjectsBasics Every object has its own variables

Classes and ObjectsBasics Every object has its own variables members

Classes and ObjectsBasics Every object has its own variables Create new ones by assigning them values

Classes and ObjectsBasics Every object has its own variables Create new ones by assigning them values class Empty(object): pass e = Empty() e.value = 123 print e.value 123

Classes and ObjectsBasics Every object has its own variables Create new ones by assigning them values class Empty(object): pass e = Empty()e2 = Empty() e.value = 123print e2.value print e.valueAttributeError: 'Empty' 123object has no attribute 'value'

Classes and ObjectsBasics The values of member variables customize objects

Classes and ObjectsBasics The values of member variables customize objects Use them in methods

Classes and ObjectsBasics The values of member variables customize objects Use them in methods class Greeter(object): def greet(self, name): print self.hello, name, '!'

Classes and ObjectsBasics The values of member variables customize objects Use them in methods class Greeter(object): def greet(self, name): print self.hello, name, '!'

Classes and ObjectsBasics Every object has its own variables Create new ones by assigning them values class Greeter(object): def greet(self, name): print self.hello, name, '!' g = Greeter()

Classes and ObjectsBasics Every object has its own variables Create new ones by assigning them values class Greeter(object): def greet(self, name): print self.hello, name, '!' g = Greeter() g.hello = 'Bonjour'

Classes and ObjectsBasics Every object has its own variables Create new ones by assigning them values class Greeter(object): def greet(self, name): print self.hello, name, '!' g = Greeter() g.hello = 'Bonjour' g.greet('Waya') Bonjour Waya !

Classes and ObjectsBasics Every object has its own variables Create new ones by assigning them values class Greeter(object): def greet(self, name): print self.hello, name, '!' g = Greeter()g2 = Greeter() g.hello = 'Bonjour'g2.hello = 'Salut' g.greet('Waya')g2.greet('Waya') Bonjour Waya !Salut Waya !

Classes and ObjectsBasics Contents of memory hello greet stackheap g Greeter 'Bonjour'

Classes and ObjectsBasics Contents of memory hello greet stackheap g Greeter self name 'Waya' 'Bonjour'

Classes and ObjectsBasics Every object's names are separate

Classes and ObjectsBasics Every object's names are separate class Greeter(object): def greet(self, name): print self.hello, name, '!' hello = 'Hola' g = Greeter() g.hello = 'Bonjour' g.greet('Waya') Bonjour Waya !

Classes and ObjectsBasics Creating objects and then giving them members is error-prone

Classes and ObjectsBasics Creating objects and then giving them members is error-prone Might forget some (especially when making changes)

Classes and ObjectsBasics Creating objects and then giving them members is error-prone Might forget some (especially when making changes) Any code repeated in two or more places…

Classes and ObjectsBasics Creating objects and then giving them members is error-prone Might forget some (especially when making changes) Any code repeated in two or more places… Define a constructor for the class

Classes and ObjectsBasics Creating objects and then giving them members is error-prone Might forget some (especially when making changes) Any code repeated in two or more places… Define a constructor for the class Automatically called as new object is being created

Classes and ObjectsBasics Creating objects and then giving them members is error-prone Might forget some (especially when making changes) Any code repeated in two or more places… Define a constructor for the class Automatically called as new object is being created A natural place to customize individual objects

Classes and ObjectsBasics Creating objects and then giving them members is error-prone Might forget some (especially when making changes) Any code repeated in two or more places… Define a constructor for the class Automatically called as new object is being created A natural place to customize individual objects Python uses the special name __init__(self,...)

Classes and ObjectsBasics A better Greeter class Greeter(object): def __init__(self, what_to_say): self.hello = what_to_say def greet(self, name): print self.hello, name, '!'

Classes and ObjectsBasics Why it's better first = Greeter('Hello') first.greet('Waya') Hello Waya !

Classes and ObjectsBasics Why it's better first = Greeter('Hello') first.greet('Waya') Hello Waya ! second = Greeter('Bonjour') second.greet('Waya') Bonjour Waya !

Classes and ObjectsBasics Contents of memory hello greet stackheap second first Greeter 'Bonjour' hello 'Hello'

Classes and ObjectsBasics A comon mistake class Greeter(object): def __init__(self, what_to_say): hello = what_to_say def greet(self, name): print self.hello, name, '!'

Classes and ObjectsBasics What goes wrong first = Greeter('Hello')

Classes and ObjectsBasics What goes wrong first = Greeter('Hello') first.greet('Waya') Attribute Error: 'Greeter' object has no attribute 'hello'

Classes and ObjectsBasics What goes wrong first = Greeter('Hello') first.greet('Waya') Attribute Error: 'Greeter' object has no attribute 'hello' self.name stores the value in the object

Classes and ObjectsBasics What goes wrong first = Greeter('Hello') first.greet('Waya') Attribute Error: 'Greeter' object has no attribute 'hello' self.name stores the value in the object name on its own is a local variable on the stack

Classes and ObjectsBasics What goes wrong first = Greeter('Hello') first.greet('Waya') Attribute Error: 'Greeter' object has no attribute 'hello' self.name stores the value in the object name on its own is a local variable on the stack class Greeter(object): def __init__(self, what_to_say) hello = what_to_say

Classes and ObjectsBasics Object data is not protected or hidden in Python

Classes and ObjectsBasics Object data is not protected or hidden in Python first = Greeter('Hello') first.greet('Waya') Hello Waya ! first.hello = 'Kaixo' Kaixo Waya !

Classes and ObjectsBasics Object data is not protected or hidden in Python first = Greeter('Hello') first.greet('Waya') Hello Waya ! first.hello = 'Kaixo' Kaixo Waya ! Some languages prevent this

Classes and ObjectsBasics Object data is not protected or hidden in Python first = Greeter('Hello') first.greet('Waya') Hello Waya ! first.hello = 'Kaixo' Kaixo Waya ! Some languages prevent this All discourage it

Classes and ObjectsBasics A more practical example class Rectangle(object): def __init__(self, x0, y0, x1, y1): assert x0 < x1, 'Non-positive X extent' assert y0 < y1, 'Non-positive Y extent' self.x0 = x0 self.y0 = y0 self.x1 = x1 self.y1 = y1

Classes and ObjectsBasics A more practical example class Rectangle(object): def __init__(self, x0, y0, x1, y1): assert x0 < x1, 'Non-positive X extent' assert y0 < y1, 'Non-positive Y extent' self.x0 = x0 self.y0 = y0 self.x1 = x1 self.y1 = y1

Classes and ObjectsBasics Benefit #1: fail early, fail often

Classes and ObjectsBasics Benefit #1: fail early, fail often # Programmer thinks rectangles are written # [[x0, x1], [y0, y1]] >>> field = [[50, 100], [0, 200]]

Classes and ObjectsBasics Benefit #1: fail early, fail often # Programmer thinks rectangles are written # [[x0, x1], [y0, y1]] >>> field = [[50, 100], [0, 200]] >>> # Class knows rectangles are (x0, y0, x1, y1) >>> field = Rectangle(50, 100, 0, 200) AssertionError: non-positive X extent

Classes and ObjectsBasics Benefit #2: readability class Rectangle(object):... def area(self): return (self.x1-self.x0)*(self.y1- self.y0) def contains(self, x, y): return (self.x0 <= x <= self.x1) and \ (self.y0 <= y <= self.y1)

Classes and ObjectsBasics Compare List of ListsObject field = [[0, 0], [100, 100]] field = Rectangle(0, 0, 100, 100) rect_area(field)field.area() rect_contains(field, 20, 25) field.contains(20, 25)

Classes and ObjectsBasics Compare List of ListsObject field = [[0, 0], [100, 100]] field = Rectangle(0, 0, 100, 100) rect_area(field)field.area() rect_contains(field, 20, 25) field.contains(20, 25) Make it even clearer by creating a Point2D class

Classes and ObjectsBasics Compare List of ListsObject field = [[0, 0], [100, 100]] field = Rectangle(0, 0, 100, 100) rect_area(field)field.area() rect_contains(field, 20, 25) field.contains(20, 25) Make it even clearer by creating a Point2D class Then re-defining Rectangle in terms of it

January 2011 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. created by Greg Wilson