Object Oriented Programming in Python

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 Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
ASP.NET Programming with C# and SQL Server First Edition
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
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
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 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
GoodOO Programming Practice in Java © Allan C. Milne v
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.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Creating Your Own Classes
Classes and Objects.
Object Oriented Programming
COMPSCI 107 Computer Science Fundamentals
Object-Oriented Programming (OOP) in Python
Objects as a programming concept
CS-104 Final Exam Review Victor Norman.
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
Chapter 3: Using Methods, Classes, and Objects
Class: Special Topics Copy Constructors Static members Friends this
More about OOP and ADTs Classes
Creating Your OwnClasses
Lecture VI Objects The OOP Concept Defining Classes Methods
Procedural Abstraction Object-Oriented Code
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
To Get Started Paper sheet
Pointers and References
Classes In C#.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Object-oriented programming
Lecture 22 Inheritance Richard Gesick.
Teach A-Level Computer Science: Object-Oriented Programming in Python
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
Object-Oriented Programming
More about OOP and ADTs Classes
Constant: value does not change
Chapter 9 Objects and Classes
Passing Parameters by value
Object-Oriented Programming
An Introduction to Object Orientated Programming
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Review of Previous Lesson
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
A Level Computer Science Topic 6: Introducing OOP
Object-Oriented Design AND CLASS PROPERTIES
Lecture 8 Object Oriented Programming (OOP)
Creating and Using Classes
Presentation transcript:

Object Oriented Programming in Python Additional Slides: Attributes, Methods and the Constructor

Recap: Modules and Import Organising a Python Program into Files

Python Modules A module is a file containing function definitions (and statements) If you import the module you can use the functions Module name is file name File lib.py def f(name): return "hello "+ name def g(i): return i+1 Import Prefix import lib print(lib.f("bill"))

Import As Module name prefix can be avoided Import only some definitions File lib.py def f(name): return "hello "+ name def g(i): return i+1 Import without prefix No Prefix from lib import f print(f("bill"))

Person class declaration … without constructor at first The Person ‘Class’ Person class declaration … without constructor at first

The Person Class (Initially without constructor) Method / Example Description p = Person Construct a new person p.setName("Bill") Set the person name p.setJob("brickie") Set the job the person does p.describe() Return a report string The Person Class (Initially without constructor) File name is class name class Person: # Initially, there is no constructor def setName(self, name): self.name = name def setJob(self, job): self.job = job def describe(self): s = "{0} works as a {1}" return s.format(self.name, self.job) File Person.py

Defining a Method in a Class class Person: # Initially, no constructor def setName(self, name): self.name = name def setJob(self, job): self.job = job Two parameters Parameter 1 Parameter 2 person-ex1.py from Person import Person p = Person() p.setName("Bill") p.setJob("plumber") print(p.describe()) Name within the object Import: from file import classname

Self class Person: # Initially, no constructor Self is NOT a keyword def setName(self, name): self.name = name Self is NOT a keyword ALWAYS use 'self' To set a name in an object of the Person class …. … set the ‘name’ field (or attribute) of the object …. … to the value given as a parameter

Attribute Getters and Setters class Person: # No constructor def setName(self, name): self.name = name def getName(self): return self.name We can have a method To set an attribute To get the value of an attribute WARNING Do not always need getters and setters Not a good way to create abstractions

Discussion: What Are the Pros/Cons of Python for Teaching? We teach programming principles ... we work with a specific language

This is a default constructor: it is created automatically Before we can ‘act’ on an object PROBLEM New person is empty Error if we 'describe' This is a default constructor: it is created automatically from Person import Person p = Person() p.setName("Bill") p.setJob("plumber") print(p.describe())

Defining a Constructor class Person: def __init__(self, n): self.name = n def getName(self): return self.name Constructor Has a special name May have parameters Don’t forget ‘self’ Constructor name Constructor parameter

Attributes – Good Practice Attributes are not declared In Python, nothing is! Good practice to initialise all attributes in the constructor Getters do not fail Clear what the attributes are Do not add more class Person: def __init__(self, n): self.name = n self.job = "unknown" self.location = "unknown" def getName(self): return self.name def getJob(self): return self.job def getLocation(self): return self.location

Python Issues for Teaching OOP Usual OOP Python The attributes are declared A class has a fixed set of attributes Attributes can be hidden: access only by methods Nothing is declared Attributes appear when assigned to Hiding is not enforced Use Python to teach OOP Avoid some Python tricks Use only a subset … explain later

Collecting Objects We can put objects in a list Must have the correct mental model for assignment

Collecting Object Example Create a list of people Create two Person objects Add to list from Person import Person people = [] bill = Person("Bill") people.append(bill) clare = Person("Clare") people.append(clare)

Collecting Object Example Create a list of people Create two Person objects Add to list Does this work? Why no need to add to list again? from Person import Person people = [] bill = Person("Bill") people.append(bill) clare = Person("Clare") people.append(clare) bill.setJob("plumber") clare.setJob("programmer")

Understanding Python Assignment Copy – WRONG Variable has value Assignment copies value Reference Variable has reference Assignment copies reference Reference  sharing In Python All variables are references But difference not noticeable when data immutable people = [ , ] name = Bill job = plumber name = Clare job = programmer bill = clare =

Summary

Summary We can create new classes Python issues: nothing declared Methods Constructor and attributes Python issues: nothing declared Enabled us to treat constructors second Collecting objects It works … need to understand assignment and references