Introduction to Object-Oriented Programming (OOP) II

Slides:



Advertisements
Similar presentations
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein Classes and Objects Object Oriented Programming.
Types in Scripting Languages CS 351 – Programming Paradigms.
Recitation 6 Programming for Engineers in Python.
Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.
Classes 2 COMPSCI 105 S Principles of Computer Science.
Unit 8 Classes and Objects; Inheritance Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
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
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)
Classes 3 COMPSCI 105 S Principles of Computer Science.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
A whole new class of programming CS 5 today HW10: Due Sun, Nov 15 Pr0: Ariane 5 Reading Pr1/Lab:the Date class Pr2 Connect4Board Pr3 Connect4Player (extra.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ if self.month in.
Introduction to Pyrex September 2002 Brian Quinlan
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer'
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
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.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
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,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due FRIDAY, Dec 4 at 4:00 pm.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Making our own Classes and objects  As in real life, we’re now creating classes of objects.  a class that defines basic characteristics and functions.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
EECS 110: Lec 14: Classes and Objects
CMSC201 Computer Science I for Majors Lecture 25 – Classes
EECS 110: Lec 15: Classes and Objects (2)
COMPSCI 107 Computer Science Fundamentals
Guide to Programming with Python
Object Oriented Programming
Classes and Objects; Inheritance
EECS 110: Lec 17: Review for the Final Exam
COMPSCI 107 Computer Science Fundamentals
Example: Vehicles What attributes do objects of Sedan have?
Software Development Java Classes and Methods
Copyright (c) 2017 by Dr. E. Horvath
Fundamentals of Programming II Interfaces and Implementations
Introduction to Object-Oriented Programming (OOP)
Introduction to Object-Oriented Programming (OOP) II
Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,
Adapted from slides by Marty Stepp and Stuart Reges
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Introduction to Object-Oriented Programming (OOP)

EECS 110: Lec 14: Classes and Objects
Fundamentals of Programming I Commonly Used Methods More Modeling
Fundamentals of Programming I More Data Modeling
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
C# Revision Cards Data types
EECS 110: Lec 15: Classes and Objects (2)
Lecture 18 Python OOP.
A Level Computer Science Topic 6: Introducing OOP
Introduction to Object-Oriented Programming (OOP)
Presentation transcript:

Introduction to Object-Oriented Programming (OOP) II

Quick review: operator overloading We have learned some basic features of OOP Constructor: def __init__(self): String representation: def __str__(self):, or def __repr__(self): Method within a class: def tomorrow(self): Object attributes (object variables …) self.year, self.month, self.day. We also discussed the topic of operator overloading

What does it mean? An operator such as ‘==‘, ‘>’ can be associated with a function to reflect its meaning. E.g., in our Date class, we have three functions is_equal(), is_before(), is_after() When comparing two Date objects, we’d say d1.is_equal(d2), d1.is_before(), d1.is_after() If we implement operator overloads for the Date class, we could have said d1 == d2, d1 < d2, d1 > d2

Overloading ‘==‘ class Date: … def __eq__(self, other): if self.year == other.year and \ self.month == other.month and \ self.day == other.day: return True else: return False If the function is_equal() has been defined, we can do … class Date: … def __eq__(self, other): if self.is_equal(other): return True else: return False class Date: … def __eq__(self, other): return self.is_equal(other)

Overloading ‘>‘ Overloading ‘>=‘ class Date: … def __gt__(self, other): return self.is_after(other) Overloading ‘>=‘ class Date: … def __ge__(self, other): return self.is_after(other) or \ self.is_equal(other)

__str__() vs __repr__() In Python, when printing an object, two methods can play roles, __str__() and __repr__() The difference is illustrated well by this post, though the syntax of the post is Python 2.x https://www.geeksforgeeks.org/str-vs-repr-in-python Let us walk through the example

The datetime class is provided by Python, in which the __repr__() and __str__() are already defined.

The Complex class is defined by the programmer (YOU The Complex class is defined by the programmer (YOU!), in which the __repr__() and __str__() are defined at your wish. Show repr-str.py

Class exercises Given a Book class as follows, define methods to overload ‘>’, ‘<‘, ‘>=‘, ‘<=‘, and ‘==‘, if the comparison is based on the attribute ‘pub_year’ class Book: def __init__( self, title, author, pub_year ): ''' Create an object self.author = author self.title = title self.pub_year = pub_year # an integer

Class exercises If the comparison is based on the attribute ‘title’, write the method that overloads ‘>’ using string comparison. class Book: def __init__( self, title, author, pub_year ): ''' Create an object self.author = author # a string self.title = title # a string self.pub_year = pub_year # an integer

Class exercises If the comparison is based on the attribute ‘pub_year’, if ‘pub_year’ is the same, then check ‘title’, if title is the same, check ‘author’. class Book: def __init__( self, title, author, pub_year ): ''' Create an object self.author = author # a string self.title = title # a string self.pub_year = pub_year # an integer

Other operator overload Python supports more operator overload __ne__ : not equal __contains__ : membership check __add__ : add to the collection (+) __iadd_ : for += See book_shelf demonstration book.py, book_shelf.py, book_shelf_app.py