PYTHON CLASSES and INHERITANCE (download slides and py files ĂŶĚ follow along) 6 0001 LECTURE 9 6 0001 LECTURE 9 1
Previous PDF | Next PDF |
[PDF] Use classes & functions defined in another file • A Python - UMBC
Use classes functions defined in another file • A Python module is a file with the same name (plus the py extension) • Like Java import, C++ include
[PDF] MIT6_0001F16_Python Classes and Inheritance
PYTHON CLASSES and INHERITANCE (download slides and py files ĂŶĚ follow along) 6 0001 LECTURE 9 6 0001 LECTURE 9 1
[PDF] Modules and Classes McGraw-Hill Education - Access Engineering
A module is just another file full of Python code, and if we just want everything in that and one class can "inherit" methods and variables from another class
[PDF] The current topic: Python Announcements Dictionaries Dictionaries
Python: Dictionaries, Files, Classes, Inheritance, Polymorphism The current topic : Another approach: iterate through the file just as you would through a list
[PDF] The current topic: Python Announcements Multiple Inheritance - CS
Dictionaries, command-line arguments, files, classes, inheritance, example, object is an ancestor of C two different ways (through A and through B) • How do
[PDF] Python Classes and Inheritance - MIT OpenCourseWare
PYTHON CLASSES and INHERITANCE (download slides and py files ĂŶĚ follow along) 6 0001 LECTURE 9 6 0001 LECTURE 9 1
[PDF] Download Object Oriented Python Tutorial - Tutorialspoint
Python has been an object-oriented language since it existed In this tutorial in Python 1 Programming languages are emerging constantly, and so are different methodologies Oriented Programming (OOP) are Inheritance, Polymorphism, Abstraction, ad A module is a Python file with some functions or variables in it
[PDF] CME 193: Introduction to Scientific Python Lecture 4: File I/O and
The file object Interaction with the file system is pretty straightforward in Python Inheritance We can define sub classes and inherit from another class
[PDF] Object oriented programming with Python - CSC
Crawl through a file reading back and forth without loading anything to List item can be any Python object, items can be of different type print "x is something completely different" The derived class “inherits” the attributes and methods
[PDF] python is not the programming language of the future
[PDF] python json tutorial pdf
[PDF] python libraries
[PDF] python machine learning by example pdf github
[PDF] python machine learning packt pdf
[PDF] python machine learning pdf raschka
[PDF] python machine learning projects
[PDF] python machine learning sebastian raschka pdf github
[PDF] python mcq online test
[PDF] python midterm exam pdf
[PDF] python mini projects with database
[PDF] python mit pdf
[PDF] python mysql connector
[PDF] python numpy partial differential equation
PYTHON CLASSES
and INHERITANCE6.0001 LECTURE 91
LAST TIME
abstract data types through classesCoordinateexample
Fractionexample
more on classesgetters and setters
information hiding
class variables
inheritance6.0001 LECTURE 92TODAY
IMPLEMENTING USING
THE CLASS vs THE CLASS
implementinga new object type with a class definethe class define data attributes (WHAT IS the object) define methods (HOW TO use the object)6.0001 LECTURE 93usingthe new object type in
code create instancesof the object type do operationswith themwrite code from two different perspectivesCLASS DEFINITION INSTANCE
OF AN OBJECT TYPE vs OF A CLASS
class name is the type class Coordinate(object) class is defined generically use selfto refer to some instance while defining the class (self.xself.y)**2 selfis a parameter to methods in class definition class defines data and methods common across all instances6.0001 LECTURE 94instance is one specific object
coord= Coordinate(1,2) data attribute values vary between instances c1 = Coordinate(1,2) c2 = Coordinate(3,4) c1 andc2have different data attribute values c1.xand c2.xbecause they are different objects instance has the structure of the classWHY USE OOP AND
CLASSES OF OBJECTS?mimic real life
group different objects part of the same type6.0001 LECTURE 95Image Credits, clockwise from top: Image Courtesy Harald Wehner, in the public Domain. Image Courtesy MTSOfan, CC-BY-NC-SA. Image Courtesy Carlos Solana, license CC-
BY-NC-SA. Image Courtesy Rosemarie Banghart-Kovic, license CC-BY-NC-SA. Image Courtesy Paul Reynolds, license CC-BY. Image Courtesy Kenny Louie, License CC-BY
WHY USE OOP AND
CLASSES OF OBJECTS?
mimic real life group different objects part of the same type6.0001 LECTURE 966.0001 LECTURE 96
Image Credits, clockwise from top: Image Courtesy Harald Wehner, in the public Domain. Image Courtesy MTSOfan, CC-BY-NC-SA. Image Courtesy Carlos Solana, license CC-
BY-NC-SA. Image Courtesy Rosemarie Banghart-Kovic, license CC-BY-NC-SA. Image Courtesy Paul Reynolds, license CC-BY. Image Courtesy Kenny Louie, License CC-BY
GROUPS OF OBJECTS HAVE
ATTRIBUTES (RECAP)
data attributeshow can you represent your object with data?
what it is
for a coordinate: x and y values
for an animal: age, name
procedural attributes (behavior/operations/methods)how can someone interact with the object?
what it does
for a coordinate: find distance between two
for an animal: make a sound
6.0001 LECTURE 97
HOW TO DEFINE A CLASS
(RECAP) class Animal(object): def__init__(self, age): self.age= age self.name = None myanimal= Animal(3)6.0001 LECTURE 98
GETTER AND SETTER METHODS
class Animal(object): def__init__(self, age): self.age= age self.name = None defget_age(self): return self.age defget_name(self): return self.name defset_age(self, newage): self.age= newage defset_name(self, newname=""): self.name = newname def__str__(self): return "animal:"+str(self.name)+":"+str(self.age) getters and setters should be used outside of class to access data attributes6.0001 LECTURE 99
AN INSTANCE and
DOT NOTATION (RECAP)
instantiation creates an instance of an object a = Animal(3) dot notation used to access attributes (data and methods) though it is better to use getters and setters to access data attributes a.age a.get_age()6.0001 LECTURE 910
INFORMATION HIDING
author of class definition may change data attribute variable names class Animal(object): def__init__(self, age): self.years= age defget_age(self): return self.years if you are accessing data attributes outside the class and class definition changes, may get errors outside of class, use getters and setters instead use a.get_age()NOT a.agegood style
easy to maintain code
prevents bugs
6.0001 LECTURE 911
PYTHON NOT GREAT AT
INFORMATION HIDING
allows you to access data from outside class definition print(a.age) allows you to write to data from outside class definition a.age= 'infinite' allows you to create data attributes for an instance from outside class definition a.size= "tiny" it's not good style to do any of these!6.0001 LECTURE 912
DEFAULT ARGUMENTS
default arguments for formal parameters are used if no actual argument is given defset_name(self, newname=""): self.name = newname default argument used here a = Animal(3) a.set_name() print(a.get_name()) argument passed in is used here a = Animal(3) a.set_name("fluffy") print(a.get_name())6.0001 LECTURE 913
HIERARCHIES
6.0001 LECTURE 914
Image Credits, clockwise from top: Image Courtesy Deeeep, CC-BY-NC. Image Image Courtesy MTSOfan, CC-BY-NC-SA. Image Courtesy Carlos Solana, license CC-BY-NC-SA.
Image Courtesy Rosemarie Banghart-Kovic, license CC-BY-NC-SA. Image Courtesy Paul Reynolds, license CC-BY. Image Courtesy Kenny Louie, License CC-BY. Courtesy