Lecture 18 Python OOP.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

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 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Classes 2 COMPSCI 105 S Principles of Computer Science.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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:
Writing Classes (Chapter 4)
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Object Oriented Programing (OOP)
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
The need for Programming Languages
COMPSCI 107 Computer Science Fundamentals
Object Oriented Programming
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Object Oriented Programming in Python: Defining Classes
COMPSCI 107 Computer Science Fundamentals
Lecture 2 Python Basics.
Introduction to Python
Lecture 4 Python Basics Part 3.
Software Development Java Classes and Methods
Copyright (c) 2017 by Dr. E. Horvath
Fundamentals of Programming II Interfaces and Implementations
Lecture VI Objects The OOP Concept Defining Classes Methods
Expressions and Control Flow in JavaScript
Engineering Innovation Center
suggested reading: Java Ch. 6
Object Oriented Programming
Creating and Deleting Instances Access to Attributes and Methods
Object Oriented Programming in Python Session -3
Topics Introduction to File Input and Output
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Lecture 4 Python Basics Part 3.
T. Jumana Abu Shmais – AOU - Riyadh
Variables Title slide variables.
Today’s topics UML Diagramming review of terms
Fundamentals of Programming I Commonly Used Methods More Modeling
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Introducing JavaScript
A Level Computer Science Topic 6: Introducing OOP
Topics Introduction to File Input and Output
Object-Oriented Design AND CLASS PROPERTIES
Introduction to Object-Oriented Programming (OOP) II
Presentation transcript:

Lecture 18 Python OOP

Python Classes class BankAccount: def __init__(self): self._balance = 0 def withdraw(self, amount): self._balance -= amount def deposit(self, amount): self._balance += amount def get_balance(self): return self._balance Shell (note that the class file was in the working directory from which I ran idle): >>> from bank_account import BankAccount >>> a = BankAccount() >>> a.deposit(500) >>> a.get_balance() 500 >>> b = BankAccount() >>> b.get_balance() >>> b._balance >>> a.withdraw(100) 400 http://anandology.com/python-practice-book/object_oriented_programming.html

__init__ __init__ is called as soon as an object is created. It is not technically a constructor, since the object already exists. However, it does the same kind of initialization work you would do in a constructor. Some sources do use the term "constructor" for __init__. If there is no need for any initialization (the type of case in which you would use an implicit constructor in Java), you can omit __init__. There is a default do-nothing __init__. Functions with names that begin and end with double underscores ("dunder methods") are hooks, methods that are called automatically in certain circumstances and can be overridden to provide customized behavior. http://www.diveintopython.net/object_oriented_framework/defining_classes.html

Python Classes Class member functions are called methods The first argument of every class method, including __init__, is always a reference to the current instance of the class. By convention, this argument is always named self. In the __init__ method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. Although you need to specify self explicitly when defining the method, you do not specify it when calling the method; Python will add it for you automatically. self is equivalent to the this pointer in C-family languages. Methods can create instance variables on the fly, without declaration outside the method, because the self. gives them class scope. A single underscore leading a variable name marks the variable as intended to be private. Python does not enforce encapsulation very rigorously; note that I was able to get directly to _balance in the shell. The term attribute is used to refer to anything that is part of an object, either a data value or a function. http://www.diveintopython.net/object_oriented_framework/defining_classes.html

One-Class OOP App See crawler.py, linked from web site. Don't run it for more than a couple of hours; your ISP or Digital Ocean will eventually throttle your bandwidth Uses three libraries, not counting sys: urllib3 http client certifi supports ssl and works with urllib3 re provides regex functionality Documentation and examples are available online for all of these Here is some help on regexes with re: https://docs.python.org/3/howto/regex.html#regex-howto

A Multi-Class Application bank_account.py: class BankAccount: def __init__(self, num): self._balance = 0 self._num = num def withdraw(self, amount): self._balance -= amount def deposit(self, amount): self._balance += amount def get_balance(self): return self._balance def get_num(self): return self._num

A Multi-Class Application bank.py: from bank_account import BankAccount class Bank: def __init__(self): self._accounts = [] def add_account(self, num): self._accounts.append(BankAccount(num)) def find_account(self, num): curr = None for index in range (len(self._accounts)): if(self._accounts[index].get_num() == num): curr = index return curr def account_deposit(self, num, amount): curr = self.find_account(num) if curr != None: self._accounts[curr].deposit(amount) def account_withdrawal(self, num, amount): return self._accounts[curr].withdraw(amount) return None def account_get_balance(self, num): return self._accounts[curr].get_balance()

A Multi-Class Application Put all three files in the same directory. To run this application from a Linux/Windows/OSX command line, type python3 bank_driver.py. Idle has a menu item to run scripts. bank_driver.py: import sys from bank import Bank def main(): b = Bank() b.add_account(1) print('$ {:.2f}'.format(b.account_get_balance(1))) b.account_deposit(1, 100.20) b.account_withdrawal(1, 50.75) if __name__ == "__main__": sys.exit(main())

Where are the Getters and Setters? Python does not have a way to create truly private variables. The leading underscore in a name is just a marker that the programmer did not intend other classes to directly access a value, and particularly to directly change it. If you use variables that you want to make freely available form outside the class, name the variables without the leading underscores and just access them directly, rather than with getter and setter functions.

__str__ __str__ is Python's equivalent of toString, somewhat like overloading << in C++ __str overloads the str() cast for the type version 1: class Widget: def __init__(self, characteristic_a, characteristic_b): self._characteristic_a = characteristic_a self._characteristic_b = characteristic_b >>> w1 = Widget(1,2) >>> w1 <widget.Widget object at 0x7f72a155ada0> version 2: def __str__(self): return 'Widget with characteristic a = ' + str(self._characteristic_a) + ' and characteristic b = ' +str(self._characteristic_b) <widget.Widget object at 0x7f687b9f11d0> >>> str(w1) 'Widget with characteristic a = 1 and characteristic b = 2'

__repr__ __repr__ is another method for representing an object as a string, but it is normally used for debugging rather than helping end-users. It should return a string containing the code necessary to recreate the object. version 5: class Widget: def __init__(self, characteristic_a, characteristic_b): self._characteristic_a = characteristic_a self._characteristic_b = characteristic_b def __eq__(self, other): if((self._characteristic_a == other._characteristic_a) and (self._characteristic_b == other._characteristic_b)): return True else: return False def __add__(self, other): return Widget(self._characteristic_a + other._characteristic_a, self._characteristic_b + other._characteristic_b) def __str__(self): return 'Widget with characteristic a = ' + str(self._characteristic_a) + ' and characteristic b = ' +str(self._characteristic_b) def __repr__(self): return 'Widget({0},{1})'.format(self._characteristic_a, self._characteristic_b) >>> from widget import Widget >>> w1 = Widget(1,2) >>> w1 Widget(1,2) >>> w2 = Widget(3,4) >>> w3 = w1 + w2 >>> w3 Widget(4,6)

Operator Overloading Note that operators like ==, <=, !=, + are equivalent to functions. For example, if x and y are values of some type: x == y is equivalent to calling a boolean function x.equals(y), which returns True if the two values are equal (according to whatever test for equality you code) and False if they are not equal. z = x + y is equivalent to calling a function that returns a value of the same type which represents the total of the two values, again according to whatever definition you code. In Python, we can overload many operators by overriding dunder methods.

Operator Overloading Without overloaded == >>> from widget import Widget >>> w1 = Widget(1,2) >>> w1 <widget.Widget object at 0x7f687b9f11d0> >>> str(w1) 'Widget with characteristic a = 1 and characteristic b = 2' >>> w2 = Widget(1,2) >>> w1 == w2 False version 3: class Widget: def __init__(self, characteristic_a, characteristic_b): self._characteristic_a = characteristic_a self._characteristic_b = characteristic_b def __eq__(self, other): if((self._characteristic_a == other._characteristic_a) and (self._characteristic_b == other._characteristic_b)): return True else: return False def __str__(self): return 'Widget with characteristic a = ' + str(self._characteristic_a) + ' and characteristic b = ' +str(self._characteristic_b) True >>> w3 = Widget(3,4) >>> w1 == w3

Operator Overloading Without overloaded + >>> w1+w2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Widget' and 'Widget' version 4: class Widget: def __init__(self, characteristic_a, characteristic_b): self._characteristic_a = characteristic_a self._characteristic_b = characteristic_b def __eq__(self, other): if((self._characteristic_a == other._characteristic_a) and (self._characteristic_b == other._characteristic_b)): return True else: return False def __add__(self, other): return Widget(self._characteristic_a + other._characteristic_a, self._characteristic_b + other._characteristic_b) def __str__(self): return 'Widget with characteristic a = ' + str(self._characteristic_a) + ' and characteristic b = ' +str(self._characteristic_b) >>> from widget import Widget >>> w1 = Widget(1,2) >>> w2 = Widget(3,4) >>> w3 = w1 + w2 >>> print(w3) Widget with characteristic a = 4 and characteristic b = 6

Text File I/O Write to a text file: import sys def main(): my_file = open('test_file.txt', 'w') # open file for writing in text, not binary, mode text = input('what do you have to say? ') size = my_file.write(text + '\n') # returns number of characters written, including the literal print('wrote ' + str(size) + ' characters') my_file.close() if __name__ == "__main__": sys.exit(main()) 'a' instead of 'w' allows you to append to an existing file

Text File I/O Read from a text file, part 1: my_file = open('test_file.txt', 'r') # open file for reading in text, not binary, mode text = my_file.read() print(text) my_file.close() If the file is reasonably large, use an iterator to read one line at a time instead of reading the whole file into memory at once: my_file = open('wordsEn.txt', 'r') for line in my_file: print(line)

Text File I/O $ cat widgets.csv 1,2 3,4 5,6 import sys Use string.split() to parse data from a text file to construct objects: $ cat widgets.csv 1,2 3,4 5,6 import sys from widget import Widget def main(): widgets = [] my_file = open('widgets.csv', 'r') # open file for reading in text, not binary, mode for line in my_file: data = line.split(',') w = Widget(data[0], data[1]) widgets.append(w) for widget in widgets: print(widget) if __name__ == "__main__": sys.exit(main())