Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Slides:



Advertisements
Similar presentations
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology
Python Objects and Classes
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Object-Oriented Programming
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
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 Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Python Programming Chapter 12: Classes and Objects Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2010/2011.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
UML Class Diagram: class Rectangle
Guide to Programming with Python
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
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.
An Object-Oriented Approach to Programming Logic and Design
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
Introduction to Python III CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
JavaScript, Fourth Edition
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
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.
Working With Objects Tonga Institute of Higher Education.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
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.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
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,
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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:
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 03 – Class Design, Object Discovery and Use Richard Salomon Centre for.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John.
Classes and Objects Digging a little deeper. Rectangles We want a class to represent a rectangle which located somewhere in XY plane. The question is,
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Exceptions and Handling
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Topics Procedural and Object-Oriented Programming Classes
Methods Attributes Method Modifiers ‘static’
University of Central Florida COP 3330 Object Oriented Programming
Class: Special Topics Copy Constructors Static members Friends this
UML Class Diagram: class Rectangle
Procedural Abstraction Object-Oriented Code
Tkinter GUIs Computer Science and Software Engineering
Object-oriented programming
Object-oriented Design in Processing
Teach A-Level Computer Science: Object-Oriented Programming in Python
Programming with Microsoft Visual Basic 2008 Fourth Edition
Object Oriented Programming in Python
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Introduction to Value-Returning Functions: Generating Random Numbers
Object-Oriented Programming
Object-oriented Design in Processing
Object-oriented Design in Processing
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1

Lesson objectives 1. Write a class definition 2. Instantiate objects 3. Assign values to attributes 4. Change attribute values 5. Copy objects 6/16/09 Python Mini-Course: Lesson 26 2

Class definition Use the class keyword Example: class Point(object): """ represents a point in 2-D space """ print Point 6/16/09 Python Mini-Course: Lesson 26 3

Instantiating an object Call the class constructor by using the class name as if it were a function Example: p1 = Point() print p1 6/16/09 Python Mini-Course: Lesson 26 4

Assigning attribute values Use the object name and the attribute name with dot notation Example: p1.x = 3.0 p1.y = 4.0 print p1.x, p1.y 6/16/09 Python Mini-Course: Lesson 26 5

Assigning attribute values These are called instance attributes p2 = Point() p2.x, p2.y = 12.0, 13.0 print p1.x, p1.y print p2.x, p2.y 6/16/09 Python Mini-Course: Lesson 26 6

Note on attributes Python handles attributes somewhat differently from other OOP languages All attributes are "public" Attributes can be created on the fly 6/16/09 Python Mini-Course: Lesson 26 7

Using attributes Object attributes are just like any other variable print '(%g, %g)' % (p1.x, p1.y) from math import sqrt distance = sqrt(p1.x**2 + p2.y**2) print distance 6/16/09 Python Mini-Course: Lesson 26 8

Note: Objects are data types You can pass objects to functions and return them from functions, just like any other data type 6/16/09 Python Mini-Course: Lesson 26 9

Example: def print_point(p): print '(%g, %g)' % (p.x, p.y) print_point(p1) NB: this example violates the principle of encapsulation 6/16/09 Python Mini-Course: Lesson 26 10

Encapsulation Examples rectangle1.py rectangle2.py rectangle3.py 6/16/09 Python Mini-Course: Lesson 26 11

Aliasing Try this: box = Rectangle(10.0, 200.0) box1 = box box1.width = print box.width, box1.width box1 is box 6/16/09 Python Mini-Course: Lesson 26 12

Copying Use the copy module import copy box2 = copy.copy(box) box2.width = 50.0 print box.width, box2.width box2 is box 6/16/09 Python Mini-Course: Lesson 26 13

Limitations of copying box2.corner.x = 2.0 print box.corner.x box2.corner is box.corner 6/16/09 Python Mini-Course: Lesson 26 14

Deep copying Use the copy module box3 = copy.deepcopy(box) box3.corner.x = 1.0 print box.corner.x, box3.corner.x box3.corner is box.corner 6/16/09 Python Mini-Course: Lesson 26 15