Similar documents
Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Programming I. Course 9 Introduction to programming

Data Structures (list, dictionary, tuples, sets, strings)

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Lecture 21. Programming with Subclasses

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Lecture 21. Programming with Subclasses

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

Lecture 18. Classes and Types

JAVA MOCK TEST JAVA MOCK TEST II

CS558 Programming Languages Winter 2013 Lecture 8

CSE 341, Autumn 2015, Ruby Introduction Summary

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

Lecture 21. Programming with Subclasses

CS558 Programming Languages

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

ENCAPSULATION AND POLYMORPHISM

Introduction to Programming Using Java (98-388)

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Data Abstraction. Hwansoo Han

C++ Important Questions with Answers

CSE 303: Concepts and Tools for Software Development

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

What are the characteristics of Object Oriented programming language?

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Abstract data types &

Abstract Data Types & Object-Oriented Programming

Arrays Classes & Methods, Inheritance

The Pyth Language. Administrivia

OBJECT ORIENTED PROGRAMMING

CS 349 / SE 382 Scripting. Professor Michael Terry March 18, 2009

Short Notes of CS201

1.00 Lecture 13. Inheritance

Java Object Oriented Design. CSC207 Fall 2014

Get Unique study materials from

61A Lecture 26. Monday, October 31

CS201 - Introduction to Programming Glossary By

CS 231 Data Structures and Algorithms, Fall 2016

BASIC CONCEPT OF OOP

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

drawobject Circle draw

Topic 9: Type Checking

Topic 9: Type Checking

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

CS Lecture 26: Grab Bag. Announcements

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

PREPARING FOR THE FINAL EXAM

Binghamton University. CS-140 Fall Dynamic Types

PREPARING FOR PRELIM 2

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

PROGRAMMING LANGUAGE 2

CSE : Python Programming

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

G Programming Languages - Fall 2012

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

Classes and Objects 1

EXCEPTIONS, CALCULATOR 10

Chapter 5 Object-Oriented Programming

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Unit E Step-by-Step: Programming with Python

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Supporting Class / C++ Lecture Notes

CIS192 Python Programming

Computational Mathematics with Python

CIS192 Python Programming

CS 5142 Scripting Languages

CS-202 Introduction to Object Oriented Programming

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Programming Language Concepts: Lecture 2

COP 3330 Final Exam Review

Classes, objects, methods and properties

STRUCTURING OF PROGRAM

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

Exceptions CS GMU

CPS 506 Comparative Programming Languages. Programming Language

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Object-Oriented Technology. Rick Mercer

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Babu Madhav Institute of Information Technology, UTU 2015

SCHEME INTERPRETER GUIDE 4

About Variables in Python F E B 1 1 T H

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Short, Unique and Mysterious

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CSE 307: Principles of Programming Languages

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Computer Components. Software{ User Programs. Operating System. Hardware

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Transcription:

https://lambda.mines.edu

Object oriented programming is a type of programming with objects that maintain internal state and pass messages between each other. First appeared in the mid-1960s in Simula: a programming language designed for discrete event simulations It should not come as a surprise that OOP was first used for simulations, the real world can easily be modeled using objects that pass messages between eachother and maintain state Smalltalk (1971) also featured OOP and really caused OOP to kick off. Go to CPW s office sometime and chat with him about Smalltalk: he really likes this language!

Classes: Classes provide a definition for the data format and the procedures available for member objects. Objects: Objects are instances of a certain class, that follow the rules defined in the class. Each of these have a variety of kinds of fields: Instance Variables: Variables which are associated with each individual object, these preform the state-maintenance we need for OOP. Class Variables: Variables which are associated with the class itself, all of the objects of that class share this variable and do not have their own copy. Likewise class/instance methods refer to the methods available from the class versus the instance.

Object-oriented programming is more than just classes and objects; it s a whole programming paradigm based around objects (data structures) that contain data fields and methods. It is essential to understand this; using classes to organize a bunch of unrelated methods together is not object orientation. Junade Ali, Mastering PHP Design Patterns

Encapsulation prevents external code from being concerned with the inner workings of an object by allowing the objects methods to define how state is manipulated. Some languages distinguish between private and public variables to specify which variables should be able to be modified by other objects, and which ones should not be. Python does this by convention: private methods and variables should be prefixed with an underscore.

Often times, one object may share variables and methods with another class. Example An Employee might inherit from a Person as: Both have variables to store first and last names They share a method to generate a full name But the Employee also has variables that make HR people happy...

Polymorphism allows methods to take objects of different types. Instances of subclasses can be called on functions that take their parent s type, for example, if load_elevator can be called on a Person, Employee inherits from Person, then load_elevator should be able to be called on an Employee instance as well. Statically typed languages often provide generics, which allows a method to be called on multiple types, even if there is not inheritance between them. An example is templates in C++. Dynamically typed languages provide a method to check if an object is an instance of another to allow the same function to take multiple types. In Python, this is isinstance.

In Python, we can use isinstance to make our functions take objects of different types: # n can be an int or a float def frobnicate(n): if isinstance(n, float): return... elif isinstance(n, int): return... raise TypeError("I only take ints and floats")

If it walks like a duck and quacks like a duck, then it must be a duck! Duck typing is a specific kind of polymorphism where we accept any object which has certain variables or methods, even if the objects do not have a common parent. In Python, we can do this with hasattr: def f(x): if hasattr(x, "walk") and hasattr(x, "quack"): x.walk() x.quack() else: raise TypeError("I only take ducks!")

To define a class in Python, use the class syntax: class Person: def init (self, fname, lname): self.fname = fname self.lname = lname The init method is the name of the magic method that Python calls to construct an instance of the class. To construct an instance, call Person(fname, lname). All instance methods (such as init ) take a reference to the instance as their first argument. By convention, this is usually named self.

Magic methods are special method names recognized by Python s internals when it needs to preform a certain action. They start and end with a double underscore. init gets called on newly constructed instances of the object. del gets called when an instance is destructed. eq gets called to test if two instances are equal. call gets called when an instance is called as a function. getitem gets called when an instance gets square brackets (such as to get an item from a list) There s plenty of more, see "Data Model" in the Python Documentation for more information.

The type written in parentheses after the name defines the base class. This class will inherit from the base class. class Employee(Person): def init (self, ssn, account, *args, **kwargs): self.ssn = ssn self.account = account super(). init (*args, **kwargs) Employee inherits from Person super() refers to the object in it s base class. In this case, we call the init of our base class with the remaining arguments.

Unlike Java, Python classes can inherit from multiple base classes, allowing common variables and methods from two classes. For example (maybe in a GUI toolkit): class Button(Rectangle, Clickable, Labeled):...

Suppose we have a base class A. We also have subclasses B and C that inherit from A. D inherits from both B and C. If B and C both override the same method from A, which method does D get? Python and Perl s solution: take the method from the first base class listed when the class was declared. C++: requires explicit statement of which class the method will dispatch from. Java: only allows single inheritance. B A D C

Our programs may encounter errors, for example, if a file was not found, or we were not able to connect to a remote server. Rather than crashing the entire program, software engineers often desire a way to clean up the error and report an error occurred.

In a C program, you might handle errors like this: /* Return code -1 = error, and errno will be set */ int f(int x) { char *A = malloc(x); /*... */ int pid = fork(); if (pid == -1) goto fail; /*... */ return z; fail: /* cleanup from error */ free(a); return -1; }

1 Return codes for errors are never standard, and the programmer always needs to look up what a function call might return. 2 If a programmer forgets to handle an error from a function they call, it could have undesired results. 3 Functions which handle errors that just want to pass the error up to their caller have to have this error handling code, even though they are not using it directly.

In Go, functions that return errors return two values: the result, and an error object: f, err := os.open("filename.ext") It is then the programmer s responsibility to handle the error, potentially returning the error to the caller: if err!= nil { return err }

1 Return codes for errors are never standard, and the programmer always needs to look up what a function call might return. Taken care of using error objects! 2 If a programmer forgets to handle an error from a function they call, it could have undesired results. Still an issue! 3 Functions which handle errors that just want to pass the error up to their caller have to have this error handling code, even though they are not using it directly. Still an issue!

Python provides an exception handling system. An exception is a special kind of object, like an error object in Go. Exceptions are raised, not returned. A try/except block can be used to intercept an exception, and preform cleanup from the error. If function only cares about passing the error to their caller, they need not write any error handling code just to pass it to their caller.

def graph(data): for point in data: if not isinstance(point, GraphPoint): raise TypeError('need GraphPoint data')... def graph_string(s): data = user_convert(s) return graph(data) def graph_from_user(): while True: try: graph_string(input('point>')) except TypeError as e: print(e)

An else block placed after a try/except will be executed only if the exception did not occur: while True: try: f = open(name) except FileNotFoundError as e: os.chdir('..') else: print('file Opened!') break

Code listed in the finally block of a try/except/(else) will always be executed, regardless of whether an exception occurs. # note that this "GUIDialog" is made up, but this # presents a real-world type usage def promptyn(prompt): try: g = GUIDialog(buttons=YESNO, text=prompt) g.show() return g.response() except ValueError: logger.log("unable to set prompt string") finally: g.close() Note that the context manager (with statement) in Python can often be used as a substitute for a finally.

Python has a large set of built-in exceptions, but when one of the built-ins is insufficient, you can subclass Exception and make your own. class PlottingError(Exception): pass Note that you should usually try and use one of exceptions the language provides rather than define your own.

1 Return codes for errors are never standard, and the programmer always needs to look up what a function call might return. Solved! 2 If a programmer forgets to handle an error from a function they call, it could have undesired results. Solved! 3 Functions which handle errors that just want to pass the error up to their caller have to have this error handling code, even though they are not using it directly. Solved!