Objects in Python: Part 2 Damian Gordon. Initialising an Object.

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
CSC 9010: Natural Language Processing
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Unit 8 Classes and Objects; Inheritance Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
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.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
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.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
Python Tutorial Lecture for EE562 Artificial Intelligence for Engineers 1.
Introduction to Pyrex September 2002 Brian Quinlan
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Make a blank window This is a starter activity and should take 5 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In script mode create a file.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
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.
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)
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
Python Conditionals chapter 5
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
JSON COMPSCI 105 SS 2015 Principles of Computer Science.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
JSON COMPSCI 105 S Principles of Computer Science.
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:
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
Xi Wang Yang Zhang. 1. Strings 2. Text Files 3. Exceptions 4. Classes  Refer to basics 2.py for the example codes for this section.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Object-Orientated Design (Summary)
Adapted from slides by Marty Stepp and Stuart Reges
COMPSCI 107 Computer Science Fundamentals
Adapted from slides by Marty Stepp and Stuart Reges
Classes and Objects; Inheritance
Classes and Objects – digging deeper
COMPSCI 107 Computer Science Fundamentals
Modules and Packages Damian Gordon.
Introduction to Python
Week 8 Classes and Objects
Adapted from slides by Marty Stepp and Stuart Reges
Computer Science 111 Fundamentals of Programming I
Tutorial Lecture for EE562 Artificial Intelligence for Engineers
Functions CIS 40 – Introduction to Programming in Python
Review Object Oriented Programming centres on objects: variables that include other variables and functions. Each object has a particular area of work.
Object-Oriented Programming
Adapted from slides by Marty Stepp and Stuart Reges
User Defined Classes CS F.
Introduction to Python
Week 8 Classes and Objects
Adapted from slides by Marty Stepp and Stuart Reges
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Object Oriented Programming in Python
Basic Inheritance Damian Gordon.
Margaret Derrington KCL Easter 2014
Classes, Objects and lists
Computer Science 111 Fundamentals of Programming I
G. Pullaiah College of Engineering and Technology
CSE 231 Lab 11.
Access Control Damian Gordon.
Lecture 18 Python OOP.
A Level Computer Science Topic 6: Introducing OOP
Python 12 Mr. Husch.
Migrating to Object-Oriented Programs
Week 2 Classes and Objects
Presentation transcript:

Objects in Python: Part 2 Damian Gordon

Initialising an Object

What if we did the following:

Initialising an Object p1 = Point() p1.x = 5 print("P1-x, P1-y is: ", p1.x, p1.y);

Initialising an Object p1 = Point() p1.x = 5 print("P1-x, P1-y is: ", p1.x, p1.y);

>>> Traceback (most recent call last): File "C:/Users/damian.gordon/AppData/ Local/Programs/Python/Python35-32/Point-error.py", line 11, in print("P1-x, P1-y is: ", p1.x, p1.y); AttributeError: 'Point' object has no attribute 'y‘ >>>

Initialising an Object So what can we do?

Initialising an Object So what can we do? We need to create a method that forces the programmers to initialize the attributes of the class to some starting value, just so that we don’t have this problem.

Initialising an Object So what can we do? We need to create a method that forces the programmers to initialize the attributes of the class to some starting value, just so that we don’t have this problem. This is called an initialization method.

Initialising an Object Python has a special name it uses for initialization methods. _ _ init _ _()

class Point: def __init__(self,x,y): self.move(x,y) # END Init def move(self,a,b): self.x = a self.y = b # END Move def reset(self): self.move(0,0) # END Reset # END Class Initialising an Object

class Point: def __init__(self,x,y): self.move(x,y) # END Init def move(self,a,b): self.x = a self.y = b # END Move def reset(self): self.move(0,0) # END Reset # END Class When you create an object from this class, you are going to have to declare initial values for X and Y.

Initialising an Object So without the initialization method we could do this: – p1 = Point() – p2 = Point() but with the initialization method we have to do this: – p1 = Point(6,5) – p2 = Point(2,2)

Initialising an Object And if we forget to include the values, what happens?

Initialising an Object And if we forget to include the values, what happens? Traceback (most recent call last): File "C:/Users/damian.gordon/AppData/Local/ Programs/Python/Python35-32/Point-init.py", line 21, in p = Point() TypeError: __init__() missing 2 required positional arguments: 'x' and 'y'

Initialising an Object But if we want to be lazy we can do the following:

Initialising an Object def __init__(self, x=0, y=0): self.move(x,y) # END Init

Initialising an Object def __init__(self, x=0, y=0): self.move(x,y) # END Init

Initialising an Object And then we can do: – p1 = Point() – p2 = Point(2,2)

Initialising an Object And then we can do: – p1 = Point() – p2 = Point(2,2) If we don’t supply any values, the initialization method will set the values to 0,0.

Initialising an Object And then we can do: – p1 = Point() – p2 = Point(2,2) If we don’t supply any values, the initialization method will set the values to 0,0. But we can also supply the values, and the object is created with these default values.

Documenting the Methods

Python is considered one of the most easy programming languages, but nonetheless a vital part of object-orientated programming is to explain what each class and method does to help promote object reuse.

Documenting the Methods Python supports this through the use of docstrings. These are strings enclosed in either quotes(‘) or doublequotes(“) just after the class or method declaration.

Documenting the Methods class Point: “Represents a point in 2D space” def __init__(self,x,y): ‘Initialise the position of a new point’ self.move(x,y) # END Init

Documenting the Methods def move(self,a,b): ‘Move the point to a new location’ self.x = a self.y = b # END Move def reset(self): ‘Reset the point back to the origin’ self.move(0,0) # END Reset

Initialising an Object Now run the program, and then do: >>> >>> help (Point)

Initialising an Object And you’ll get: Help on class Point in module __main__: class Point(builtins.object) | Represents a point in 2D space | | Methods defined here: | | calc_distance(self, other_point) | Get the distance between two points | | move(self, a, b) | Move the point to a new location | | reset(self) | Reset the point back to the origin |

etc.