By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Python Objects and Classes
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Introduction to Python
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Python Control of Flow.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
The Ruby Programming Language
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.
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Introduction to Python III CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Python iterators and generators. Iterators and generators  Python makes good use of iterators  And has a special kind of generator function that is.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Iterators and Generators Thomas Wouters XS4ALL
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
+ Ruby and other programming Languages Ronald L. Ramos.
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Introduction to Python III CIS 391: Artificial Intelligence Fall, 2008.
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
LECTURE 6 Advanced Functions and OOP. FUNCTIONS Before we start, let’s talk about how name resolution is done in Python: When a function executes, a new.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Dr. Philip Cannata 1 Python Overview. Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Rajkumar Jayachandran.  Classes for python are not much different than those of other languages  Not much new syntax or semantics  Python classes are.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Object Oriented Programming in Python: Defining Classes
Object-Oriented Programming (OOP) in Python
Copyright (c) 2017 by Dr. E. Horvath
Functions.
Lecture VI Objects The OOP Concept Defining Classes Methods
Engineering Computing
Creating and Deleting Instances Access to Attributes and Methods
Python Classes By Craig Pennell.
CHAPTER FOUR Functions.
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Object-Oriented Programming
Python Primer 2: Functions and Control Flow
Substitution in string value
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Python LinkedLists.
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Python iterators and generators
Python Reserved Words Poster
Presentation transcript:

By: Chris Harvey Python Classes

Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes NameObject a5 b8 c13 NameObject x8 fibonaccifunction result21 NameObject absfunction rangefunction True1

Scopes Region of code with access to a specific namespace At any point, several scopes can be accessed The most local scope is accessed first NameObject a5 b8 c13 NameObject x8 fibonaccifunction result21 NameObject absfunction rangefunction True1

Class Definition Syntax Definition formed by a block of statements A new namespace is created during class definition A class must be defined before usage class Person: name = 'John Smith' age = 20 def SayName(self): print(self.name) def HowOld(self): return self.age

Class Objects Class definition itself has attributes Class definition can be instantiated Initialization methods can be added to class definitions Person.name #Alice person = Person() def __init__(self, newName): self.name = newName person = Person('Bob')

Instance Objects All attributes of class object exist on instance Instance can have attributes that are different from the class definition instance.tempMember = 5 print(instance.tempMember) del instance.tempMember

Method Objects Methods can be referred to by a name and called later When a method is called the object it is called on is passed as first argument By convention, the calling object argument is named self speak = person.SayName() speak() def SayName(self): print(self.name)

Inheritance A class can inherit the attribute of another class or classes A name is matched to the first base class found with depth-first, then left-to- right Types can be checked using isinstance(object,type) and issubclass(type,baseType) class Animal: #definition pass class Dog(Animal): #definition pass class Bird: #definition pass class FlyingDog(Dog,Bird): #definition pass

Private Variables No such thing as truly private variables By convention, an underscore denotes a private member Two underscores prefixed and up to one suffixed indicates name mangling class Person: name = 'Alice' age = 20 __secret_ = 'A private variable' print(Person._Person__secret_)

Iterators Classes that represent collections can offer iterators Special __iter__ and __next__ methods must be created for traversing collection Next() raises a StopIteration exception when no more elements exist def __iter__(self): return iterationObject def __next__(self): if(lastElement): raise StopIteration else: return nextElement

Generators Creation of iterators can be simplified Use keyword yield to designate elements of collection All iterator functionality created automatically def Squares(n): for i in range(n): yield i*i for s in Squares(3): print(s)