Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Python Objects and Classes
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
Week 3: Classes, constructors, destructors, new operator Operator and function overloading.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
Object Oriented Programing (OOP)
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
ISBN Object-Oriented Programming Chapter Chapter
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
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.
Class Fundamentals BCIS 3680 Enterprise Programming.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Creating Your Own Classes
Object Oriented Programming
Topics Procedural and Object-Oriented Programming Classes
CS-104 Final Exam Review Victor Norman.
Intro To Classes Review
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Classes In C#.
Teach A-Level Computer Science: Object-Oriented Programming in Python
Chapter 9 Objects and Classes
Defining Classes and Methods
Object-Oriented Programming
2.1 Introduction to Object-Oriented Programming
Programming For Big Data
Presentation transcript:

Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined classes / objects Class Object Python’s self parameter Overrideable methods __init__ and __str__ Accessor and mutator methods (“Getter” and “Setter” methods) Class diagrams in UML

Classes are everywhere …

Python programs A Python program is a sequence of Python statements in one or more files When a Python program is executing, virtually everything in a Python program is an object Each object has a type Program output :

Object and Class Object’s type = Class used to create the object Object can also be referred to as an instance of a class.

Each object has … A type (class that created it) Identity (location in computer memory) Data attributes (variables in a class) and/or methods (functions in a class) Special data attributes and/or methods prefixed and suffixed with a double underscore (e.g. __doc__)

Variables and objects Variables are names associated with objects The object's data attributes and methods can be accessed using the dot operator with the name of the data attribute or method dot operator

Special data attributes and methods Data attributes (e.g. __doc__, __class__, etc) Methods (e.g. __add__, __str__, etc) NOTE: Good Ref:

Mutable and Immutable objects If object data attributes can be changed, the object is mutable If object data attributes cannot be changed, the object is immutable

One object with many names Assignment of variables to the same values means the variables will be associated with the same objects (same identity and type) Output:

Variables with immutable and mutable types Assigning variables of immutable types to another and then changing either value creates a new object Assigning variables of mutable types to one another and then changing either value will modify the value of both mutableimmutable

The is operator and isinstance function The is operator checks to see if the identity of the operands is the same The isinstance(object, class | type | tuple) built-in function returns True or False

To get a list of standard Python classes, etc. help('__builtin__') provides list of built-in classes, functions, and data

Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined classes / objects Class Object Python’s self parameter Overrideable methods __init__ and __str__ Accessor and mutator methods (“Getter” and “Setter” methods) Class diagrams in UML

Proc vs OOP Procedural ProgrammingObject Oriented Programming (OOP) Good for simple programs with simple flows and models Good for complex programs with complex models and interactions. Not good for modelling real-world objects and interactions. Overkill for simple programs.

Entity representation Procedural ProgrammingObject Oriented Programming (OOP) Entities represented by groups of variables Entities represented by abstractions of real-world objects Abstraction captures only those details about an object that are relevant to the current perspective.

Entity representation Procedural ProgrammingObject Oriented Programming (OOP) Entity data represented by variablesEntity data represented by data attributes defined in the class Abstraction captures only those details about an object that are relevant to the current perspective.

Methods Procedural ProgrammingObject Oriented Programming (OOP) Operations represented by functions available at module level Operations represented by object methods available at object level Object properties and methods are encapsulated by the object instance.

Program flow Procedural ProgrammingObject Oriented Programming (OOP) Program flow controlled by order of setting variables, calling functions, and passing data between functions. Procedural-OOP Hybrid : Program flow controlled by order of creating objects, calling its methods, and setting its properties. OOP : Program flow controlled by messages sent between objects and/or events raised by an object, code that responds to those events.

OOP Concepts - Class A class is a template for an object. The class defines the data attributes (properties) and methods (functions in a class) that will be common to all objects created from it. Create user-defined classes using class keyword.

OOP Concepts - Object Instance of a class created when the class definition is executed. Every instance of the class will have the same attributes -- properties and methods -- but can have different data (property values)

Python’s (self) parameter It is a parameter that gets assigned “automatically” to the object being processed. “self” is the first, and sometimes only, parameter in methods Name “self” is Python convention. Could be anything.

__init__ ( self [,params] ) It is the first method run when the class is called. The initialization method or constructor. Actually, __new__ is called first but it is rarely used and it passes everything along to __init__. Technically, __new__ and __init__ is the constructor. Most classes override__init__.

__str__( self ) Another of many methods that are common to all classes in Python that you can override (replace default behaviour with your own) Others include: __len__ __add__ __eq__ __lt__ etc.

Accessor (“getter”) method Control getting property values (object data) Without accessor: __private With accessor:

Mutator (“setter”) method Control setting property values (object data)

New (2.6+) approach to properties Old approach New approach Calls to getter functions

Class Diagram UML Graphical display of the contents of a class Dice color : str value : int roll() : None Class name Properties (data attributes) Methods Key

Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined classes / objects Class Object Python’s self parameter Overrideable methods __init__ and __str__ Accessor and mutator methods (“Getter” and “Setter” methods) Class diagrams in UML