Name Spaces. Introduction into Python Python 5: Classes, Exceptions, Generators and more. Classes: Example. Classes: Briefest Introduction



Similar documents
Python for Rookies. Example Examination Paper

The C Programming Language course syllabus associate level

Chapter 2 Writing Simple Programs

Introduction to Programming Languages and Techniques. xkcd.com FULL PYTHON TUTORIAL

Curriculum Map. Discipline: Computer Science Course: C++

Python Loops and String Manipulation

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

Moving from CS 61A Scheme to CS 61B Java

APScheduler Documentation

Glossary of Object Oriented Terms

Certified PHP Developer VS-1054

Java Application Developer Certificate Program Competencies

2! Multimedia Programming with! Python and SDL

PHP Code Design. The data structure of a relational database can be represented with a Data Model diagram, also called an Entity-Relation diagram.

Python Standard Library: Data Representation 4-1

6.170 Tutorial 3 - Ruby Basics

MA-WA1920: Enterprise iphone and ipad Programming

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Exercise 4 Learning Python language fundamentals

CS 1133, LAB 2: FUNCTIONS AND TESTING

A skip list container class in Python

Java Interview Questions and Answers

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

FLORIDA STATE COLLEGE AT JACKSONVILLE COLLEGE CREDIT COURSE OUTLINE. Introduction to Programming with Visual Basic.NET

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

Bioinformatics using Python for Biologists

CS 111 Classes I 1. Software Organization View to this point:

Fundamentals of Java Programming

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Appendix... B. The Object Constraint

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

CSE 308. Coding Conventions. Reference

Specialized Programme on Web Application Development using Open Source Tools

Assuming the Role of Systems Analyst & Analysis Alternatives

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Advanced Bash Scripting. Joshua Malone

Open source framework for interactive data exploration in server based architecture

Some programming experience in a high-level structured programming language is recommended.

Concepts and terminology in the Simula Programming Language

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Microsoft Windows PowerShell v2 For Administrators

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Java Classes. GEEN163 Introduction to Computer Programming

Python Objects. Charles Severance

Introduction to Logging. Application Logging

Commercial Database Software Development- A review.

An Incomplete C++ Primer. University of Wyoming MA 5310

Introduction to: Computers & Programming: Input and Output (IO)

Charles Dierbach. Wiley

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

The CLIPS environment. The CLIPS programming language. CLIPS production rules language - facts. Notes

Self-review 9.3 What is PyUnit? PyUnit is the unit testing framework that comes as standard issue with the Python system.

Brent A. Perdue. July 15, 2009

Java Program Coding Standards Programming for Information Technology

Programming and Software Development (PSD)

The Smalltalk Programming Language. Beatrice Åkerblom

The P3 Compiler: compiling Python to C++ to remove overhead

UML for C# Modeling Basics

Click Stream Data Analysis Using Hadoop

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

BCS2B02: OOP Concepts and Data Structures Using C++

OBJECT ORIENTED EXTENSIONS TO SQL

Java SE 8 Programming

Iteration CHAPTER 6. Topic Summary

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

Chapter 4 State Machines

Introduction to Python

Chapter 8: Bags and Sets

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

The Django web development framework for the Python-aware

Using Files as Input/Output in Java 5.0 Applications

Instruction Set Architecture of Mamba, a New Virtual Machine for Python

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Software Construction

Java CPD (I) Frans Coenen Department of Computer Science

USC VITERBI SCHOOL OF ENGINEERING INFORMATICS PROGRAM

Bitter, Rick et al "Object-Oriented Programming in LabVIEW" LabVIEW Advanced Programming Techinques Boca Raton: CRC Press LLC,2001

: provid.ir

UML Diagram Types. Use Cases do the Following. Use Case Diagram

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Exercise 1: Python Language Basics

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

Database Management System Choices. Introduction To Database Systems CSE 373 Spring 2013

Python Programming: An Introduction to Computer Science

Python Classes and Objects

Computing Concepts with Java Essentials

Programming and Software Development CTAG Alignments

Formal Engineering for Industrial Software Development

Chapter 6 Load Balancing

Chapter 13: Program Development and Programming Languages

Object Relational Database Mapping. Alex Boughton Spring 2011

The Little Go Book is licensed under the Attribution-NonCommercial-ShareAlike 4.0 International license. You should not have paid for this book.

I don t intend to cover Python installation, please visit the Python web site for details.

Transcription:

Name Spaces Introduction into Python Python 5: Classes, Exceptions, Generators and more Daniel Polani Concept: There are three different types of name spaces: 1. built-in names (such as abs()) 2. global names of a module 3. local names in functions same names in different name spaces denote different objects, they have no relation with each other. Remember, a variable instantiated (brought to life) in a function is local to this function. Likewise, a name defined in a given module needs to be prefixed by the module name and the. qualifier. Daniel Polani: Introduction into Python p.1/?? Daniel Polani: Introduction into Python p.2/?? Classes: Briefest Introduction Classes: Example Idea: the concept of a class is at the core of object-oriented programming. A class defines a type of objects and operators that can be applied to this type of objects. It becomes particularly powerful through the concept of inheritance which allows one to group object types according to operations which can be applied to them in a similar coherent fashion. answer = print dt.respond() # class global variable # always use self as first method argument # DeepThought.answer would also have worked # as a class-global variable # create an instance of DeepThought # call the respond method of # DeepThought for instance dt Daniel Polani: Introduction into Python p.3/?? Daniel Polani: Introduction into Python p.4/??

Classes: Initialization Classes: Initialization Example Construction: in general, classes need to be put in a defined initial state before use. In C++, this is done with a concept called constructors, but in Python, the mechanism is different. Use init(self) method to define the initializer. More Details: introduce method such as: if you want to set an object-local variable, you have to qualify it by self. Full Example: answer = self.thinking += 1 # initialize object-local variable # modify this variable, only for # this object # inherits from class-global # one object dt2 = DeepThought() # another object of same type print dt.thinking # object local print dt.respond(), dt.thinking, dt2.thinking # all object-local # variables print dt.respond(), dt.thinking, dt2.thinking dt.answer += 17 print dt.respond(), dt.thinking, dt2.thinking 0 1 0 2 0 59 3 0 Classes: Methods call Methods Daniel Polani: Introduction into Python p.5/?? Inheritance Daniel Polani: Introduction into Python p.6/?? answer = print "think..." self.think() # respond calls think Consider: we can create more specific classes from more general classes through inheritance a more general class class Computer: "The general computer class." def init (self, name): self.calculate = 0 self.name = name self.calculate += 1 # sets computer name to parameter print dt.respond() think... Daniel Polani: Introduction into Python p.7/?? Daniel Polani: Introduction into Python p.8/??

Inherited Class Destructors class DeepThought(Computer): "A particular computer." Computer. init (self, "DeepThought") # initialize the Computer in DeepThought-type computers print "think..." print self.answer print dt.calculate print dt.name # if that was not given, # it would just increment # self.calculate (try it!) think... 0 # notice that DeepThought.think has been used here! DeepThought Destructor: method with fixed internal name del() is called when class is being destroyed, either explicitly (via del operator) or just by ending the scope of the variable. class Computer: print "..." class DeepThought(Computer): def del (self): print "DeepThought crashed..." # DeepThought is generated # now dt is being removed, as its life span terminates... DeepThought crashed... Pretty-Printing Classes Daniel Polani: Introduction into Python p.9/?? Some Notes on Classes Daniel Polani: Introduction into Python p.10/?? Use: repr method self.answer = 0 def repr (self): return " " + str(self.answer) + " " print dt # return a string Private Variables: are denoted by prefix. This is a convention and there is a way of addressing these variables. Nevertheless, if a variable is private, there is probably a good reason for that, and you should not consider it editable from outside (only class methods should be manipulating it). Class Variables: come into existence dynamically by assignment. Daniel Polani: Introduction into Python p.11/?? Daniel Polani: Introduction into Python p.12/??

pickle pickle II pickle: Example: module supporting the persistent storage of data structures import pickle a = (1,2,3,[5,6,7]) pickled_a = pickle.dumps(a) print "a=", a, "\n" print "pickled a=", ", pickled_a, ", "\n" print "unpickled a=", pickle.loads(pickled_a) you can store the string to a file, completely storing a data structure in your program storage on harddisk to recover it at a later time. This language feature is called persistence. In Java, this would be called serialization. will yield a= (1, 2, 3, [5, 6, 7]) pickled a= " (I1 I2 I3 (lp0 I5 ai6 ai7 atp1. " unpickled a= (1, 2, 3, [5, 6, 7]) Daniel Polani: Introduction into Python p.13/?? Daniel Polani: Introduction into Python p.14/?? Pickling works on Classes Exceptions import pickle self.answer = 0 def repr (self): return " " + str(self.answer) + " " print "Before", dt pickled_thought = pickle.dumps(dt) unpickled_thought = pickle.loads(pickled_thought) print "After", unpickled_thought # return a string Usage: Exceptions are classes and can be used in different ways, e.g. with a specifying string to indicate what is wrong raise Exception, "nonsense" File "<stdin>", line 44, in? Exception: nonsense a more specific exception helps in identifying the problem more narrowly raise ValueError, File "<stdin>", line 44, in? ValueError: However: it is not a good idea to extend the existing exception class hierarchy by too much of your own fancy exceptions unless absolutely necessary. Before After Daniel Polani: Introduction into Python p.15/?? Daniel Polani: Introduction into Python p.16/??

Iterators and yield Iterator: Example Iterations: consistent use in different contexts for element in [1, 2, 3]: print element for element in (1, 2, 3): print element for key in { one :1, two :2}: print key for char in "123": print char for line in open("myfile.txt"): print line General Principle: whatever the container object (i.e. list, dictionary or other, it provides an iterator created by an iter() function which has a next() method which is called on each iteration. Once finished, it will throw a StopIteration exception. Input >>> s = abc >>> it = iter(s) >>> it <iterator object at 0x00A1DB50> a b c File "<stdin>", line 1, in? it.next() StopIteration Iterator: Implementation Daniel Polani: Introduction into Python p.17/?? Generators (yield) Daniel Polani: Introduction into Python p.18/?? class Reverse: "Iterator for looping over a sequence backwards" def init (self, data): self.data = data self.index = len(data) def iter (self): # that is called, e.g. via iter(), # or indirectly via a for loop return self def next(self): if self.index == 0: raise StopIteration self.index = self.index - 1 return self.data[self.index] >>> for char in Reverse( spam ):... print char... m a p s Input Generators: a powerful way to create iterators! Advantages: Easier to use, almost like writing a function def reverse(sequence): i = len(sequence) - 1 while i >= 0: yield sequence[i] i -= 1 4 3 2 1 for s in reverse([1,2,3,4]): print s the iterator generated by reverse remembers the states it has generated for each iteration Bottom-Line: you can separate head and body of an iteration, one of the nicest properties of Python! Daniel Polani: Introduction into Python p.19/?? Daniel Polani: Introduction into Python p.20/??