Computer Languages I
Much of the OOP story in Python boils down to this expression: object. Allows you to customize the inherited classes to do something different.
CityGML Specification
4 avr. 2012 This document is an OGC Member approved international standard. ... _CityObject which is a subclass of the GML class _Feature.
Java Programming Lab Manual
Inheritance Tree. C++ creates a new inheritance tree always. Java uses a single inheritance tree always because all classes are the child of Object class in
Generating Code with IBM Rational Rhapsody
classes if you selected class A. Note that when the Derived option is used Rational Rhapsody ignores the code generation scope that was defined on the.
AP® Computer Science A Picture Lab Student Guide
If you copy the Java source files to another folder you must copy these gif files as well. Keep the images folder and the classes folder together in the
Download Object Oriented Python Tutorial (PDF Version)
That is a thing or action is present in different forms or ways. One good example of polymorphism is constructor overloading in classes. Page 11. OOP in Python.
The BlueJ Tutorial
The distribution file for MacOS is called BlueJ-xxx.zip where xxx is a version shown as an arrow
CME 193: Introduction to Scientific Python Lecture 4: File I/O and
We can instantiate a file object using open or file Another fast option to read a file ... We can define sub classes and inherit from another class.
BE (CSE) - IVSemester (A & B-Batch) 18CSPC405 – Python
Python – Class definition Inheritance
Design Patterns Elements of Reusable Object-Oriented Software
Adapter (139) Convert the interface of a class into another interface clients inheritance to compose classes while the Structural object patterns ...
CS134: Classes Objects and Inheritance - Computer Science
•Inheritanceis the capability of one class to derive or inheritthe properties from another class •The bene?ts of inheritance are: •Often represents real-world relationships well •Provides reusability of code so we don’t have to write the same code again and again •Allows us to add more features to a class without modifying it
Python Multiprocessing - University of Colorado Boulder
inherit from it We will take the common behavior and functionality and put it in that class Every other class will inherit what is common and add more functionality if needed ( something is better than nothing ) • Python 2 7 added module called ab that should provide the abstract class notion However I do not like the way they implement it
Python Classes and Objects - George Mason University
• Python attributes and methods are public by default –public attributes: any other class or function can see and change the attribute myCircle radius = 20 –public method: any other class or function can call the method myCircle method1() • Make things private by adding __ (two underscores) to the beginning of the name:
Chapter 13 Inheritance and Polymorphism - Calvin University
Inheritance allows a programmer to separate those attributes and behaviors that are shared between vehicle types and those that are unique to each particular type The shared features are collected in a single class known as the parent or superclassand the unique features are separated into the child or subclasses
Python Review Session - Stanford University
Different versions of Python Countless Python packages and their dependencies Different projects require different packages Even worse different versions of the same package! Solution Keep multiple Python environments that are isolated from each other Each environment Can use different Python version
Inheritance is the capability of one class to acquire
In C++ we have 5 different types of Inheritance Namely 1 Single Inheritance 2 Multiple Inheritance 3 Hierarchical Inheritance 4 Multilevel Inheritance 5 Hybrid Inheritance (also known as Virtual Inheritance) 3 Single Inheritance In this type of inheritance one derived class inherits from only one base class
Searches related to python inherit class from another file filetype:pdf
In this exercise we’ll study how a class can inherit another class in Python Programs BankPolymorphic py and Windows py are examples in which inheritance is used Derive another class named AnotherDate from class ISODate You can write the new AnotherDate class before the "main program" after the import statements The AnotherDate
CME 193: Introduction to Scientific Python
Lecture 4: File I/O and Classes
Sven Schmit
stanford.edu/~schmit/cme1934: File I/O and Classes4-1
Feedback form
Please take a moment to fill out feedback form
http://goo.gl/forms/NBkBXWgnCCNote: link also on couse website.
4: File I/O and Classes4-2
Contents
File I/O
Classes
Exercises
4: File I/O and Classes4-3
File I/O
How to read from and write to disk.
4: File I/O and Classes4-4
The file object
Interaction with the file system is pretty straightforward in Python. Done usingfile objectsWe can instantiate a file object usingopenorfile4: File I/O and Classes4-5Opening a file
f = open(filename, option)filename: path and filename option: "r" read file "w" write to file "a" app endto file We need to close a file after we are done:f.close()4: File I/O and Classes4-6 with open() as f Very useful way to open, read/write and close file:withopen ("data/text_file.txt"," r")asf: printf.read()4: File I/O and Classes4-7Reading files
read() Read entire line (o rfirst ncharacters, if supplied) readline()Reads a single line p ercall
readlines()Returns a list with lines (splits at newline)
Another fast option to read a filewithopen ("f.txt"," r")asf: forlineinf: printline4: File I/O and Classes4-8Reading files
read() Read entire line (o rfirst ncharacters, if supplied) readline()Reads a single line p ercall
readlines() Returns a list with lines (splits at newline) Another fast option to read a file with open ("f.txt"," r")asf: forlineinf: printline4: File I/O and Classes4-9Writing to file
Usewrite()to write to a filewithopen (filename," w")asf: f.write( Hello n .format(name))4: File I/O and Classes4-10More writing examples
write elements of list to file with open (filename," w")asf: forxinxs: f.write( n .format(x)) write elements of dictionary to file with open (filename," w")asf: fork, vind.iteritems(): f.write( n .format(k, v))4: File I/O and Classes4-11Contents
File I/O
Classes
Exercises
4: File I/O and Classes4-12
Defining our own objects
So far, we have seen many objects in the course that come standard with Python.IntegersStrings
ListsDictionaries
etc But often one wants to build (much) more complicated structures.4: File I/O and Classes4-13
Defining our own objects
So far, we have seen many objects in the course that come standard with Python.IntegersStrings
ListsDictionaries
etc But often one wants to build (much) more complicated structures.4: File I/O and Classes4-14
Hangman example
Objects:Game
Agents (different versions)
4: File I/O and Classes4-15
Object Oriented Programming
Express computation in terms of objects, which are instances of classes ClassBluep rint(only one)
Object
Instance (many)
Classes specify attributes (data) and methods to interact with the attributes.4: File I/O and Classes4-16
Object Oriented Programming
Express computation in terms of objects, which are instances of classes ClassBluep rint(only one)
Object
Instance (many) Classes specify attributes (data) and methods to interact with the attributes.4: File I/O and Classes4-17
Python"s way
In languages such as C++ and Java: data protection with private and public attributes and methods.Not in Python: only basics such as inheritance.
Don"t abuse power: works well in practice and leads to simple code.4: File I/O and Classes4-18
Simplest example
define class classLeaf: pass instantiate object leaf = Leaf() printleaf __main__ Leaf instance at 0 x10049df80 >4: File I/O and Classes4-19Initializing an object
Define how a class is instantiated by defining the__init__method. Seasoned programmer: in Python only one constructor method.4: File I/O and Classes4-20
Initializing an object
The init orconstructor method.classLeaf:
def__init__(self, color): self.color = color# private attribute redleaf = Leaf( red blueleaf = Leaf( blue printredleaf.color red Note how weaccessobjectattributes.4: File I/O and Classes4-21 SelfTheselfparameter seems strange at first sight.
It refers to the the object (instance) itself.
Henceself.color = colorsets the color of the objectself.color equal to the variablecolor.4: File I/O and Classes4-22Another example
Classes havemethods(similar to functions)classStock(): def__init__(self, name, symbol, prices=[]): self.name = name self.symbol = symbol self.prices = prices defhigh_price(self): if len (self.prices) == 0: return"MISSINGPRICES " return max (self.prices) apple = Stock( Apple APPL , [500.43, 570.60])printapple.high_price()Recall:list.append()ordict.items(). These are simply class methods!4: File I/O and Classes4-23
Another example
Classes havemethods(similar to functions)classStock(): def__init__(self, name, symbol, prices=[]): self.name = name self.symbol = symbol self.prices = prices defhigh_price(self): if len (self.prices) == 0: return"MISSINGPRICES " return max (self.prices) apple = Stock( Apple APPL , [500.43, 570.60])printapple.high_price()Recall:list.append()ordict.items(). These are simply class methods!4: File I/O and Classes4-24
Class attributes
classLeaf: n_leafs = 0 class attribute shared def__init__(self, color): self.color = color# object attributeLeaf.n_leafs += 1
redleaf = Leaf( red blueleaf = Leaf( blue printredleaf.color red printLeaf.n_leafs2 Class attributes are shared among all objects of that class.
4: File I/O and Classes4-25
Class hierarchy through inheritance
It can be useful (especially in larger projects) to have a hierarchy of classes.ExampleAnimal
Bird HawkSeagull
Pet Dog Cat4: File I/O and Classes4-26
Inheritance
Suppose we first define an abstract classclassAnimal: def__init__(self, n_legs, color): self.n_legs = n_legs self.color = color defmake_noise(self): print"noise"4: File I/O and Classes4-27Inheritance
We can define sub classes and inherit from another class.classDog(Animal): def__init__(self, color, name):Animal.__init__(self, 4, color)
self.name = name defmake_noise(self): print self .name +" :" + " woof" bird = Animal(2, white bird.make_noise() noise brutus = Dog( blackBrutus
brutus.make_noise()Brutus
woof shelly = Dog( whiteShelly
shelly.make_noise()Shelly
woof 4: File I/O and Classes4-28Base methods
Some methods to override__init__: Constructor__repr__: Represent the object (machine)__str__: Represent the object (human)__cmp__: Compare4: File I/O and Classes4-29
Example
Implementing Rational numbersclassRational:
pass4: File I/O and Classes4-30 SetupWhat information should the class hold?Numerator
Denominator
4: File I/O and Classes4-31
SetupWhat information should the class hold?Numerator
Denominator
4: File I/O and Classes4-32
InitImplement the__init__methodclassRational:
def__init__(self, p, q=1): self.p = p self.q = q4: File I/O and Classes4-33 InitImplement the__init__methodclassRational:
def__init__(self, p, q=1): self.p = p self.q = q4: File I/O and Classes4-34Issues
Issues?classRational:
def__init__(self, p, q=1): self.p = p self.q = qIgnore the division by 0 for now, more on that later.4: File I/O and Classes4-35
Issues
Issues?classRational:
def__init__(self, p, q=1): self.p = p self.q = qIgnore the division by 0 for now, more on that later.4: File I/O and Classes4-36
Greatest common divisor
1020and12 are the same rational. Implement agcd(a, b)function that computes the greatest common divisor ofaandb.defgcd(a, b): ifb == 0: returna else: returngcd(b, a%b)Exercise: Verify Euclidean Algorithm
4: File I/O and Classes4-37
Greatest common divisor
classRational: def__init__(self, p, q=1): g = gcd(p, q) self.p = p / g self.q = q / gWhy is this awesome?4: File I/O and Classes4-38
Representing your class: Operator overloading
Implement__repr__or__str__early toprint
Debugging
4: File I/O and Classes4-39
Operator overloading: adding two Rationals
Add Rationals just like Ints and Doubles?
Rational(10,2) + Rational(4,3)
To use+, we implement the__add__methodclassRational: def__add__(self, other): p =self.p * other.q + other.p *self.q q =self.q * other.q returnRational(p, q) ... 4: File I/O and Classes4-40Operator overloading: Comparing
__cmp__compares objectsIfselfis smaller thanother, return a negative valueIfselfandotherare equal, return 0Ifselfis larger thanother, return a positive value4: File I/O and Classes4-41
More on Operator Overloading
To learn more:
Google 'Python operator overloading".
4: File I/O and Classes4-42
Contents
File I/O
Classes
Exercises
4: File I/O and Classes4-43
Exercises
See course website for exercises for this week.
Get to know the person next to you and do them in pairs!Let me know if you have any question
quotesdbs_dbs20.pdfusesText_26[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