User Defined Classes CS100 2017F.

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

Python Objects and Classes
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
Guide to Programming with Python
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
4.1 Instance Variables, Constructors, and Methods.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
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,
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Adapted from slides by Marty Stepp and Stuart Reges
Lab Demo 1 Dr. Mai Elshehaly.
Topics Introduction to Functions Defining and Calling a Void Function
Abstract Data Types Programmer-created data types that specify
Week 8 Classes and Objects
Topics Procedural and Object-Oriented Programming Classes
COMP 170 – Introduction to Object Oriented Programming
CS-104 Final Exam Review Victor Norman.
Computer Science 111 Fundamentals of Programming I
Functions CIS 40 – Introduction to Programming in Python
Functions.
Lecture VI Objects The OOP Concept Defining Classes Methods
Object Oriented Programming
Object-Oriented Programming
Python Classes By Craig Pennell.
CS 100: Roadmap to Computing
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Python Primer 2: Functions and Control Flow
Adapted from slides by Marty Stepp and Stuart Reges
IFS410: Advanced Analysis and Design
CS 100: Roadmap to Computing
Week 8 Classes and Objects
Week 3 Object-based Programming: Classes and Objects
Adapted from slides by Marty Stepp and Stuart Reges
Classes, Objects and lists
Computer Science 111 Fundamentals of Programming I
Functions Christopher Harrison | Content Developer
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
Nate Brunelle Today: Functions
Migrating to Object-Oriented Programs
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Functions John R. Woodward.
Using Modules.
Presentation transcript:

User Defined Classes CS100 2017F

Why Classes? Sometimes it is natural to bundle data and behaviors together (technical term: encapsulation) Python supports this bundling by allowing the programmer to define a class (like, for example, the str class) A class may have arbitrarily many instances this allows all members of the class to have uniform characteristics for example, the class Dog might have instances fido, barker, and scrawny

Example: The Point Class We can define a class that describes points in the plane with this code class is a Python keyword (required) Point is the name of the class (by convention, it is capitalized) the colon (:) introduces the indented body of the class definition the first thing in the class definition should be a short docstring class Point: ''' Represents a point in a Euclidean plane '''

Creating a Point A class typically has a method (function) named __init__ that is called to create an object in the class the method name __init__ by convention has two leading and and two trailing underscores to distinguish it from other identifiers self refers to the object created. The other parameters define the initial state of the object class Point: ''' Represents a point in a Euclidean plane ''' def __init__(self, x_coor, y_coor): self.x = x_coor self.y = y_coor

Creating a Module for a Class Though it is not required, a class definition is usually saved in a separate .py file that can be imported as a module e.g., the class Point would be saved in a file named point.py the point module could then be imported and used to create points (think turtle module and turtle.Turtle() ) the module may contain related classes (Turtle and Screen) methods and data are accessed by the dot (.) operator >>> import point >>> point.Point <class 'point.Point'> >>> center = point.Point(0,0) >>> center.x

Creating a Point Additional behaviors for a point object can be defined by adding methods to the class class Point: ''' Represent a point in a Euclidean plane ''' def __init__(self, x_coor, y_coor): self.x = x_coor self.y = y_coor def coordinates(self): ''' Return a tuple of the x,y coordinates of a point ''' return (self.x, self.y) def move_to(self, x_coor, y_coor): ''' Assign new coordinates to a point '''

Extend the Point Class Write a method named move that moves a point relative to its current location (follow the pattern of the move_to method) Write a method named distance_to that calculates the distance between the current point and some other point. The code below shows correct input and output for these methods >>> import point >>> center = point.Point(0,0) >>> point_a = point.Point(1,1) >>> center.distance_to(point_a) 1.4142135623730951 >>> point_a.move(5,3) >>> point_a.coordinates() (6, 4)

Class Data A class may have data associated with it that applies to every object in the class For example, the dimension of every point is 0. This should be defined inside the class, but outside any method. class Point: ''' Represents a point in a Euclidean plane ''' dimension = 0 >>> import point >>> a_point = point.Point(2,2) >>> point.Point.dimension >>> a_point.dimension

Sugar Knows Frisbee