Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Modules and Objects Introduction to Computing Science and Programming I.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Python Programming Chapter 12: Classes and Objects Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2010/2011.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
VBA Modules, Functions, Variables, and Constants
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
ASP.NET Programming with C# and SQL Server First Edition
UML class diagrams (1) UML = Unified Modeling Language We use only class diagrams, not other UML diagrams Purpose: –keep OO concepts separate from implementation.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Review of C++ Programming Part II Sheng-Fang Huang.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Chapter 6: A First Look at Classes
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Input, Output, and Processing
Working With Objects Tonga Institute of Higher Education.
ECE122 Feb. 22, Any question on Vehicle sample code?
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
1 CSC241: Object Oriented Programming Lecture No 02.
Working With Objects Tonga Institute of Higher Education.
Object Oriented Programing (OOP)
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Programming (OOP) in Python References: Chapter 8.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
The Object-Oriented Thought Process Chapter 03
Introduction to Computing Science and Programming I
Python programming - Defining Classes
Topics Designing a Program Input, Processing, and Output
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Creating Objects & String Class
JavaScript: Functions.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
User Defined Classes CS F.
Topics Designing a Program Input, Processing, and Output
Python Primer 1: Types and Operators
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Object Oriented Programming in java
CISC101 Reminders Assignment 3 due today.
Presentation transcript:

Classes and Objects The basics

Object-oriented programming Python is an object-oriented programming language, which means that it provides features that support objected-oriented programming.

User-defined compound data types We’ve already seen classes like str, int, float and turtle. Now ready to create our own user-defined class.

Point Consider the concept of a mathematical point. In two dimensions, a point is two numbers(coordinates) that are treated collectively as a single object. Points are often written in parentheses with a comma separating the coordinates. For example, (0, 0) represents the origin, and (x, y) represents the point x units to the right and y units up from the origin.

Operation on Points Some of the typical operations that one associates with points might be calculating the distance of a point from the origin, or from another point, or finding a midpoint of two points, or asking if a point falls within a given rectangle or circle.

Operation on points A natural way to represent a point is with two numeric values. The question, then, is how to group these two values into a compound object. The quick solution is to use a tuple, and for some applications that might be a good choice.

Operation on points An alternative is to define a new class. We’ll want our points to each have an x and a y attribute.

User-defined compound data types

Initializer method Every class should have a method with the special name __init__. The initializer method is a automatically called whenever a new instance of Point is created. It give opportunity to set up the attributes required within the new instance by giving them their initial state/values. The self (any other name) parameter is automatically set to reference the newly created object that needs to be initialized.

Initializer method __init__ doesn’t create the object (i.e. set aside memory for it), — it just initializes the object to its factorydefault settings after its creation.

Create more than one object

Create new objects The variables p and q are assigned references to two new Point objects.

Constructor A constructor is a function that creates a new object instance. Every class automatically provides a constructor function which is named the same as the class

Constructor

classes A class is a factory for making objects. Every time we call the constructor, we’re asking the factory to make a new object. As the object comes off the production line, its initialization method is executed to get the object properly set up with it’s factory default settings.

Instantiation The combined process of “make me a new object” and “get its settings initialized to the factory default settings” is called instantiation.

Attributes Object instances have – attributes – methods

Attributes We can modify the attributes in an instance using dot notation.

Atrribute The variable p refers to a Point object, which contains two attributes. x --> 4 y --> 8 p 

Attribute The expression p.x means, “Go to the object p refers to and get the value of x”.

Use dot notation as part of any expression

Improving our initializer Code to create a point at position (7,5)

Placing extra parameters into the __init__ method to make class constructor more general The x and y parameters are both optional. If the caller does not supply arguments, they’ll get the default values of 0.

Improved class

Writing docstring so that it makes the most sense when it pops up as the tooltip to guide the programmer who is using class constructor.

Adding other methods to a class

Instances as arguments and parameters

Converting an instance to a string

Python has a clever trick up its sleeve to fix this. If we call new method __str__ instead of to_string, the Python will use new code __str__ whenever it need to convert a Point to a string.

Instances as return values Functions and methods can return instances

from midpoint function to halfway method

Assign each point to a variable