CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Advertisements

Object-Oriented Programming
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
C++ Classes & Data Abstraction
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
1 Advanced Material The following slides contain advanced material and are optional.
Object-oriented Programming Concepts
C++ fundamentals.
The Python Programming Language Matt Campbell | Steve Losh.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Object Oriented Software Development
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Python Programming Chapter 14: Classes and Methods Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
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.
Introduction to Object-Oriented Programming Lesson 2.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Object Oriented Programming In Python
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Python – May 19 Review –What is the difference between: list, tuple, set, dictionary? –When is it appropriate to use each? Creating our own data types:
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,
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Exceptions and Handling
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Rajkumar Jayachandran.  Classes for python are not much different than those of other languages  Not much new syntax or semantics  Python classes are.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Guide to Programming with Python
Programming Logic and Design Seventh Edition
The Movement To Objects
Classes and Objects – digging deeper
Copyright (c) 2017 by Dr. E. Horvath
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Section 11.1 Class Variables and Methods
Introduction to Object-oriented Program Design
Object Oriented Programming
3 Fundamentals of Object-Oriented Programming
Object-oriented Design in Processing
Teach A-Level Computer Science: Object-Oriented Programming in Python
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS2011 Introduction to Programming I Objects and Classes
Object-oriented Design in Processing
An Introduction to Object Orientated Programming
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
CIS16 Application Development and Programming using Visual Basic.net
Object-Oriented Programming
Object-oriented Design in Processing
Programming For Big Data
Introduction to Object-Oriented Programming (OOP)
Introduction to Object-Oriented Programming (OOP) II
Object-oriented Design in Processing
Presentation transcript:

CLASSES Python Workshop

Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics.  It is a mixture of the class mechanisms found in C++ and Modula-3.  Python classes provide all the standard features of Object Oriented Programming

Objects and Classes  An object is the basic Python building block, the 'lego brick' with which every program is built. All the elements that we've met up until now - integers, strings, lists, functions etc. - they're all objects.  A class is simply a user-defined object that lets you keep a bunch of closely related things "together".

Object Oriented Programming  Inheritance  A way to compartmentalize and reuse classes/objects already defined  Encapsulation  Grouping together of things that logically belong to each other  Polymorphism  the ability to create a variable, a function, or an object that has more than one form

Class foo # Class foo # class Foo: def __str__(self): return "I am an instance of Foo!!!" #main program f = Foo() print f

Class Components  Class class-identifier class Foo:  Constructor  special function of the class that is called whenever we create a new instance of the class def __str__(self):  Return return "I am an instance of Foo!!!"

Instantiation and Call Note how we instantiated the class with f = Foo() This we now have a new object f which has characteristics of this class, and we can use f print f

Illustration  The following illustration is simple to follow. It is based on our understanding of complex numbers that have two components  Real part  Imaginary part  Let’s see how we can work this!!

Class Complex class Complex: def __init__(self, realpart, imagpart): self.r = realpart self.i = imagpart x = Complex(3.0, -4.5) print “x real =“, x.r print “x imaginary = “, x.i

Explanation  As you can see, the definition of the class class Complex: def __init__(self, realpart, imagpart): self.r = realpart self.i = imagpart  The instantiation of the object x x = Complex(3.0, -4.5)  The use of the object attributes to print the respective parts print “x real =“, x.r print “x imaginary =“, x.i

Question  Can you see how the constructor is used to determine the parameters passed to the class when an object is instantiated?  def __init__(self, realpart, imagpart):  x = Complex(3.0, -4.5)

Question  Can you see how the attributes are used? self.r = realpart self.i = imagpart  And from the main program print “x real =“, x.r print “x imaginary =“, x.i