Class 24: Programming with Objects University of Virginia cs1120 David Evans.

Slides:



Advertisements
Similar presentations
OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Cs2220: Engineering Software Class 2: Introduction Java Fall 2010 University of Virginia David Evans.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 9: Low-Level Programming
David Evans CS200: Computer Science University of Virginia Computer Science Class 29: Vocational Skills How to Build a.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
1 CSC 221: Computer Programming I Fall 2004 course overview  what did we set out to learn?  what did you actually learn?  where do you go from here?
A First Program Using C#
Comp1004: Introduction I Welcome!. Welcome to Programming Principles Dr. David Millard Dr. Julian Rathke Dr.
CSE 425: Intro to Programming Languages and their Design A Few Key Ideas No particular language is a prerequisite for this course –However you should be.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Python – May 11 Briefing Course overview Introduction to the language Lab.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
Class 25: Python, Objects, Bombs, and Inheritance University of Virginia cs1120 David Evans.
Types in programming languages1 What are types, and why do we need them?
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Lecture 3: Rules of Evaluation CS150: Computer Science
OOP Basics Classes & Methods (c) IDMS/SQL News
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: Vocational Skills (How to Build.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Introduction to Python
Topic: Conditional Statements – Part 1
CSC 221: Computer Programming I Spring 2010
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Topic: Python’s building blocks -> Variables, Values, and Types
CSC 221: Computer Programming I Fall 2005
CS 326 Programming Languages, Concepts and Implementation
Class 29: Vocational Skills How to Build a Dynamic Web Site
The Metacircular Evaluator
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
The Metacircular Evaluator
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CISC101 Reminders All assignments are now posted.
Lecture 3: Rules of Evaluation CS200: Computer Science
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
The return Statement © 2018 Kris Jordan.
Presentation transcript:

Class 24: Programming with Objects University of Virginia cs1120 David Evans

Menu Python Programming with Objects Inheritance

Why learn Python?

Reason 1: Vocational Skill Job listings at monster.com (20 October 2009) KeywordAll USVirginia Only Typical Salary Python52738$60-180K Java $70-140K SQL $60-150K Scheme830$ K

“Scheme” Jobs

Reason 1: Vocational Skill Job listings at monster.com (20 October 2009) KeywordAll USVirginia Only Typical Salary Python52738$60-180K Java $70-140K SQL $60-150K Scheme830$ K

Reason 2: Expanding Minds The more languages you know, the more different ways you have of expressing ideas and for thinking about (and solving) problems.

“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. Printed by Ottaviano Dei Petrucci in 1501 (first music with movable type)

J S Bach, “Coffee Cantata”, BWV 211 (1732) “Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. (1501)

Reason 3: Deepening Understanding By seeing how the same concepts we encountered in Scheme are implemented by a different language, you will understand those concepts better (especially procedures, assignment, data abstraction).

Reason 4: Building Confidence By learning Python (mostly) on your own, the next time you encounter a problem that is best solved using a language you don’t know, you will be confident you can learn it (rather than trying to use the wrong tool to solve the problem). This is also important for taking cs2110 this Spring: assumes you can learn Java on your own.

Reason 5: Fun Programming in Python is fun (possibly even more fun than programming in Scheme!) Especially because: It is an elegant and simple language Most programs mean what you think they mean It is dynamic and interactive It can be used to easily build web applications It is named after Monty Python’s Flying Circus It was designed by someone named Guido.

Python A universal programming language – Everything you can compute in Scheme you can compute in Python, and vice versa – Chapter 11/PS7: implement a Scheme interpreter in Python – Chapter 12: more formal definition of a universal PL Imperative Language – Designed to support a programming where most of the work is done using assignment statements: x = e Object-Oriented Language – All data are objects – Built in support for classes, methods, inheritance

Learning New Languages Syntax: Where the {,%;!,$, etc. all go If you can understand a BNF grammar, this is easy (Okay, it still takes some getting used to a new syntax…) Semantics: What does it mean Learning the evaluation rules Harder, but most programming languages have very similar evaluation rules (but the subtle differences can cause lots of problems) Style: What are the idioms and customs of experienced programmers in that language? Takes many years to learn - need it to be a “professional” Python programmer, but not to make a useful program

Python If Instruction ::= if ( Expression ) : Block Consequent else: Block Alternate Instruction ::= if ( Expression ) : Block Consequent else: Block Alternate Evaluate Expression. If it evaluates to a true value, evaluate the Block Consequent ; otherwise, evaluate the Block Alternate. Similar to (if Expression (begin Block Consequent ) (begin Block Alternate )) Differences: Indenting and new lines matter! Changing the indentation changes meaning of code What is a “true value”: Scheme: anything that is not false. Python: anything that is not False, None, 0, an empty string or container

If Example if []: print "Empty is true!" else: print "Empty is false!" Empty is false!

Learning Python We will introduce (usually informally) Python constructs in class as we use them, example code in PS6 The “Schemer’s Guide to Python” is an introduction to Python: covers the most important constructs you need for PS6, etc. Course book: Chapter 11 introduces Python – Read ahead Section 11.1 On-line Python documentation

Making Objects In Washington, it’s dog eat dog. In academia, it's exactly the opposite. Robert Reich class Dog: def bark(self): print “wuff wuff wuff wuff” ClassDefinition ::= class Name: FunctionDefinitions

Making a Dog class Dog: def bark(self): print "wuff wuff wuff wuff" spot = Dog() AssignmentStatement ::= Variable = Expression Python assignments are like both define and set!. If the Variable name is not yet defined, it creates a new place. The value in the named place is initialized to the value of the Expression.

Python Procedures class Dog: def bark(self): print "wuff wuff wuff wuff" FunctionDefinition ::= def Name ( Parameters ): Block Parameters ::= ε | SomeParameters SomeParameters ::= Name | Name, SomeParameters Block ::= Statement Block ::= indented (Statements) Statements ::= Statement MoreStatments MoreStatements ::= ε | Statement MoreStatments FunctionDefinition ::= def Name ( Parameters ): Block Parameters ::= ε | SomeParameters SomeParameters ::= Name | Name, SomeParameters Block ::= Statement Block ::= indented (Statements) Statements ::= Statement MoreStatments MoreStatements ::= ε | Statement MoreStatments

Some Python Procedures FunctionDefinition ::= def Name ( Parameters ): Block Parameters ::= ε | SomeParameters SomeParameters ::= Name | Name, SomeParameters Block ::= Statement Block ::= indented (Statements) Statements ::= Statement MoreStatments MoreStatements ::= ε | Statement MoreStatments FunctionDefinition ::= def Name ( Parameters ): Block Parameters ::= ε | SomeParameters SomeParameters ::= Name | Name, SomeParameters Block ::= Statement Block ::= indented (Statements) Statements ::= Statement MoreStatments MoreStatements ::= ε | Statement MoreStatments def square(x): return x * x def bigger(a,b): if a > b: return a else: return b

Whitespace Matters! def bigger(a,b): if a > b: return a else: return b def bigger(a,b): if a > b: return a else: return b def bigger(a,b): if a > b: return a else: return b def bigger(a,b): if a > b: return a else: return b File " ", line 4 else: ^ IndentationError: unindent does not match any outer indentation level Python requires you to format your code structurally!

Barking: Invoking Methods class Dog: def bark(self): print "wuff wuff wuff wuff" spot = Dog() spot.bark(“Hello”) wuff wuff ApplicationStatement ::= Name ( Arguments ) Arguments ::= ε | MoreArguments MoreArguments ::= Argument, MoreArguments MoreArguments ::= Argument Argument ::= Expression. ( ) Invoke method on obj. The obj will be the first (self) parameter to the method.

Object Lingo “Apply a procedure” :: “Invoke a method” We apply a procedure to parameters. We invoke a method on an object, and pass in parameters.

Dogs with Names class Dog: def __init__(self, n): self.name = n __init__ is a constructor It creates a new object of the type. It is called when Dog(n) is evaluated. __init__ is a constructor It creates a new object of the type. It is called when Dog(n) is evaluated. spot = Dog(“Spot”) spot.name Spot bo = Dog(“Bo”) bo.name Bo

Summary An object packages state and procedures. A class provides procedures for making and manipulating a type of object. The procedures for manipulating objects are called methods. We invoke a method on an object. Friday: Inheritance Monday: Excursion on Exponential Growth – Please ready Tyson essay before Monday!