Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Python Objects and Classes
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
ASP.NET Programming with C# and SQL Server First Edition
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Python, Part 2. Python Object Equality x == y x is y In Java: (x.equals(y)) (x == y)
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
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.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
Chapter 10 Classes and Objects: A Deeper Look Visual C# 2010 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Extra Stuff for CS106 Victor Norman CS106. break and continue break and continue are both ways to alter the flow of a loop Can be used only inside a loop.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Learners Support Publications Constructors and Destructors.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
6. Classes & Objects Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Constructors and Destructors
Python programming - Defining Classes
Scope History of Ruby. Where can you use Ruby? General Features.
George Mason University
Object Oriented Programming in Python: Defining Classes
Static data members Constructors and Destructors
Object-Oriented Programming (OOP) in Python
Example: Vehicles What attributes do objects of Sedan have?
Software Development Java Classes and Methods
CS-104 Final Exam Review Victor Norman.
12. Classes & Objects Let's Learn Python and Pygame
Constructors & Destructors
This pointer, Dynamic memory allocation, Constructors and Destructor
Creating and Deleting Instances Access to Attributes and Methods
CHAPTER FIVE Classes.
Basic C++ What’s a declaration? What’s a definition?
Python’s Errors and Exceptions
Simple Classes in C# CSCI 293 September 12, 2005.
Call Frames Final Review Spring 2018 CS 1110
Finalization 17: Finalization
Learning Objectives Classes Constructors Principles of OOP
Week 8 Classes and Objects
Object Oriented Programming in Python
12. Classes & Objects a class factory makes objects + functions
Constructors and destructors
Constructors and Destructors
Simple Classes in Java CSCI 392 Classes – Part 1.
Python Programming Language
By Ryan Christen Errors and Exceptions.
A Level Computer Science Topic 6: Introducing OOP
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Introduction to Object-Oriented Programming (OOP) II
Presentation transcript:

Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation, but its interesting anyway. You can write nice OOP applications as long as you don't cheat.

Basic Class Syntax class Name: [body] Example with constructor class Rectangle: def __init__(self,x,y): # parameterize constructor self.width=x #declare instance variable width self.height=y #declare instance variable height rect=Rectangle(2,5) # allocates rectangle instance print rect.width,rect.height #hmmmm

Class Variables class Rectangle: count=0 #class variables declared here def __init__(self,x,y): Rectangle.count+=1 self.width=x self.height=y r= Rectangle(3,4) s= Rectangle(9,4) t= Rectangle(3,6) print Rectangle.count Outputs 3

Define output format of instance class Complex: def __init__(self,r,i): self.real=r self.imag=i def __str__(self):#this is how it prints if self.imag<0: return str(self.real)+str(self.imag)+"i" else: return str(self.real)+'+'+str(self.imag)+"i" x=Complex(-1,-2) y=Complex(2,5) print x,y # uses __str__ to display the instances Output is -1-2i 2+5i

Add method for Complex class Complex: def __init__(self,r,i): self.real=r self.imag=i def Add(self,c): return Complex(self.real+c.real,self.imag+c.imag) def __str__(self):#this is how it prints if self.imag<0: return str(self.real)+str(self.imag)+"i" else: return str(self.real)+'+'+str(self.imag)+"i" x=Complex(-1,-2) y=Complex(2,5) z=x.Add(y) print z Output is 1+3i

Destructors ?? Destructors are a big deal in C++ but not as much so in Python. Why? Well Python has automatic garbage collection using reference counting. This does not mean there is no need for destructors. You may want to clean up other allocated things like sockets and database connections to be closed, files, buffers and caches flushed and a few more resources that need to be released when an object is done with them.

Destructor Example class FooType(object): def __init__(self, id): self.id = id print self.id, 'born' def __del__(self): print self.id, 'died' def make_foo(): print 'Making...' ft = FooType(1) print 'Returning...' return ft print 'Calling...' ft = make_foo() print 'End...' Note: Python won't clean up an object when it goes out of scope. It will clean it up when the last reference to it has gone out of scope. Here is its output Calling... Making... 1 born Returning... End... 1 died Note2: Don’t use destructors unless you really need to.

Longer Example import datetime class Person: def __init__(self,name): self.name = name try: lastBlank = name.rindex(' ') self.lastName = name[lastBlank+1:] except: self.lastName = name self.birthday = None def getName(self): return self.name def setBirthday(self,birthdate): self.birthday = birthdate def getAge(self): if self.birthday == None: raise ValueError return (datetime.date.today()-self.birthday).days def __str__(self): return self.name me =Person('Thomas Adams') me.setBirthday(datetime.date(1956,1,31)) print me,'is',me.getAge(),'days old' Thomas Adams is 21418 days old