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] Things to know about super
Bugs of unbound super objects in earlier versions of Python the Python web site, just as I had published my essay on multiple inheritance In Python 3 0 all
[PDF] The current topic: Python Announcements Multiple Inheritance
This means that a class can have multiple parent classes 3 Fall 2008 Python: Multiple Inheritance, Parameters and Arguments, List Comprehensions
[PDF] Lecture 22: Subclasses & Inheritance - Cornell CS
28 avr 2020 · CS 1110 Introduction to Computing Using Python Solution: Create a parent class with shared code ▫ Then, create subclasses of the parent class ▫ A subclass deals with specific details different from the parent class 3
[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] Python Classes and Inheritance - MIT OpenCourseWare
WHICH METHOD TO USE? • subclass can have methods with same name as superclass • for an instance of a class,
[PDF] 6189 Homework 4 Written Exercises - MIT OpenCourseWare
18 jan 2011 · Exercise 4 4 – Designing Your Own Inheritance For this exercise, we want you to describe a generic superclass and at least three subclasses What does the code print out? (Try figuring it out without running it in Python) 3
[PDF] Chapter 5 Object Oriented Programming in Python 12 Marks
Python supports OOP concepts such as Inheritance, Method Overriding, Data abstraction and print(s1 x," ",s1 y) s2=sample() s2 y=4 print(s2 x," ",s2 y) Output : 2 3 2 4 this derived class has been inherited is the base class or parent class
[PDF] Object-Oriented Design with Python
definition, inheritance, multiple inheritance, accessibility, polymorphism realize OOP method between python and other OOP language Attributes and methods in superclass From the example Everywhere is polymorphism in Python (3)
[PDF] Ch9: Object-oriented programming - UiO
19 oct 2019 · inherit attributes and methods from parent classes A typical strategy OO is less important in Python than in C++, Java and C#, so the benefits of 3 f(x − h) ) + O(h3), and we can easily make a module that offers all of them:
[PDF] ini shared path nsclient++ ini
[PDF] initial basic feasible solution in lpp
[PDF] initial basic feasible solution in operation research
[PDF] initial basic feasible solution ppt
[PDF] initial basic feasible solution simplex method
[PDF] initialize 2d array in js
[PDF] initialize 2d array java
[PDF] initialize array in jsp
[PDF] initialize array in react js
[PDF] initialize http client java
[PDF] initialize private static variable c++
[PDF] initialize struct in class constructor
[PDF] injective homomorphism example
[PDF] injective linear transformation
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