PYTHON CLASSES and INHERITANCE define methods (HOW TO use the object) 6 0001 LECTURE 9 3 using the new object type in for a coordinate: find distance between two • for an animal: make a sound 6 0001 LECTURE 9 7
Previous PDF | Next PDF |
[PDF] MIT6_0001F16_Python Classes and Inheritance
PYTHON CLASSES and INHERITANCE define methods (HOW TO use the object) 6 0001 LECTURE 9 3 using the new object type in for a coordinate: find distance between two • for an animal: make a sound 6 0001 LECTURE 9 7
[PDF] Python Classes and Inheritance - MIT OpenCourseWare
(HOW TO use the object) 6 0001 LECTURE 9 3 using the new object type in find distance between two • for an animal: make a sound 6 0001 LECTURE 9 7
[PDF] Object-Oriented Design with Python
of Object-Oriented A D and emphasize on OOP programming with python • Introduces Python's special methods to realize class definition, inheritance
[PDF] Multiparadigm Programming with Python 3 Chapter 7
6 sept 2018 · 7 Python 3 Object Orientation 2 7 5 3 Multiple Inheritance Now consider the following Python 3 REPL session with the above definition
[PDF] The current topic: Python Announcements Multiple Inheritance - CS
3 Fall 2008 Python: Multiple Inheritance, Parameters and Arguments, List Comprehensions Multiple g(7, y=1, x=0) # TypeError: multiple values for x g(4, 6
[PDF] Download Object Oriented Python Tutorial - Tutorialspoint
Oriented Programming (OOP) are Inheritance, Polymorphism, Abstraction, ad 7 Note: Classes are preferred over modules because you can reuse them as they are Step 3: After you have installed, you should now see the Python menu as
[PDF] 25 Inheritance and Related OOP* Ideas - Cornell CS
Closely follows Chapter 18 in Think Python 4 Remove a card Things to do with a hand of cards: 1 Compare 2 Sort* 3 a [4, 9, 1, 3, 7, 10, 5, 6, 8, 2]
[PDF] Ch7: Introduction to classes (part 2) - UiO
17 oct 2019 · more about inheritance next week In Python 2, including the inheritance from object gives some extra funcionality to our class In Python 3 it is
[PDF] inherited uml
[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
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