You are on page 1of 20
Cyne ey UMSI Python Objects Pee Python for Everybody DAW comet) yes ion yes Cyne ey rong MAE laaliild * This lecture is very much about definitions and mechanics for objects * This lecture is a lot more about “how it works” and less about “how you use it” + You won't get the entire picture until this is all looked at in the context of a real problem + So please suspend disbelief and learn technique for the next 40 or so slides.. Cyne ey yes rong 5. Data Structures 5.1. More on Lists “Te st datatype has some more methods Here ae al ofthe methods of i SH the given it Equant o ata) suat, Snsert (is) Insert an item ata gen postion. The fst argument i the index ofthe element before which to inser, so +. renave(s) poptti) Remove the iter a the given position in thelist and return tf no index is specie, a:pop() removes and returns the latter i the ls (The square brackets around the / inthe method signature note that parameters optional, ot tat you should type square bracket at that postion. You wil ee thi notation Cyne ey Pata’ rong ried by Cal Oe ee Lets Start with Programs yes Cyne ey rong Europe floor? 0 creer ra ab et) we ate eao) US floor 1 Cera reset) eee print('US floor', usf) sees eo yes Cyne ey rong Object Oriented * A program is made up of many cooperating objects °* Instead of being the “whole program” - each object is a little “island” within the program and cooperatively working with other objects. * A program is made up of one or more objects working together - objects make use of each other’s capabilities yes Cyne ey rong Object * An Object is a bit of self-contained Code and Data + Akey aspect of the Object approach is to break the problem into smaller understandable parts (divide and conquer) + Objects have boundaries that allow us to ignore un-needed detail + We have been using objects all along: String Objects, Integer Objects, Dictionary Objects, List Objects... Pata’ acess evervnopy ME Objects get created and used Pata’ acess evervnopy ME Code/Data Code/Data Objects are Le) ey mexole[—) Elite Mel-ic} yes Cyne ey rng Objects hide detail - they allow us to ignore the detail of AMT program”. Pata’ acess ary Inp Objects hide detail - | y Code/Data Hac WVar-l lV MNS of the program” to Code/Data ignore the detail | Output | F-) ofl aa on 7 Bae pied Cyne ey ion Definitions Class - a template Method or Message -A defined capability of a class Field or attribute- A bit of data in a class Object or Instance - A particular instance of a class yes Cyne ey rong Terminology: Class Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). http://en.wikipedia.org/wiki/Object-oriented_programming yes rong Cyne ey Terminology: !nstance One can have an instance of a class or a particular object. The instance is the actual object created at runtime. In programmer jargon, the Lassie object is an instance of the Dog class. The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that's defined in the object's class. Object and Instance are often used interchangeably. http://en.wikipedia.org/wiki/Object-oriented_programming yes Cyne ey rong Terminology: Method An object's abilities. In language, methods are verbs. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie's methods. She may have other methods as well, for example sit() or eat() or walk() or save_timmy(). Within the program, using a method usually affects only one particular object; all Dogs can bark, but you need only one particular dog to do the barking Method and Message are often used interchangeably. http://en.wikipedia.org/wiki/Object-oriented_programming Cyne ey yes ion Some Python Objects oe ro >>> type(x) PE tars Poesy Tee) coos Tesy naeeerces) Pose Tes Eas rT ars oanaear ress) csr Tes) Pomme aes >>> dir(x) femme cc vee COR Tr eC Ct ee Poe Sem SS eR csr Peictet mE SCT er re, SCE Ce ‘rpartition’, ‘rsplit', ‘rstrip', ‘split’, Poets eeCr irr sts tts ris ss ere Tae ‘title’, ‘translate’, ‘upper’, ‘zfill'] Ear reso) (oo eC ee ee ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, sey >>> dir(z) [-, ‘clear’, ‘copy’, ‘fromkeys', ‘get’, ‘items’, PSs SR es er Sem areas] el yom hee ERODE A Sample Class 7 Peeugis Cyne ey ion Acknowledgements / Contributions ‘Thes slde are Copyright 2010- Charles R. Severance (wvw.de: ccuck.com) ofthe University of Michigan School of Information Perr ae toe ee License, Please maintain this last slide n all copies of the Peete et cat acs Cee roe rae ee ee) ceo Initial Development: Charles Severance, University of Michigan Panay een yes Cyne ey ion Additional Source Information *Snowman Cookie Cutter” by Didriks is licensed under CC BY +Photo from the television program Lassie. Lassie watches as Jeff (Tommy Rettig) works on his bike is Public Domain

You might also like