Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Day Date Topic Textbook for today Textbook-assign 2 Thurs 2016.1.21 Python Chapter 9.1 through 9.5 same Buffalo Nickel Moment Show the random selector program first. A. Scope of names Scope – where things are visible (things = names) Also talk about global code, i.e. code inside a file but not inside a function Global vrs. Local Class vrs. Instance Global variables and global functions exist outside of any other functions or classes. Local variables are only visible inside functions. Parameters are always local. Inside a fucntion… If you use a name, Python assumes it is global and looks for it outside the function. If you assign to a name, Python assumes it is a local and creates a new local version. If you want to assign to a global variable, you must first declare it as a global. names = ["Mark", "Sally", "Kathy"] def silly(s): if s in names: print ("We know",s) names = ["Mark", "Sally", "Kathy"] def silly(s): names = ["weird", "stuff", s] names = ["Mark", "Sally", "Kathy"] def silly(s): global names names = ["weird", "stuff", s] names = ["Mark", "Sally", "Kathy"] def silly(s): names[0] = s Since names is used in silly but not assigned to, Python looks for names outside, and it uses the global variable. Since you assign to names inside the function, Python thinks names is a local. So the names inside is NOT the same as names outside, and no change occurs. Here you are assigning but you have to tell Python that the names you are assigning to inside the function is the same as the one outside. Here you are not assigning to names itself, but are actually changing the existing value in the variable, so global is not needed. Python assumes you mean the global names. B. Object Oriented programming Part 1: evolution of ideas Arrays were the first composite objects. Parallel arrays were the first form of quasi-database. Objects are homogeneous in arrays (all the same) at least the same size. Nobody talked about "objects" in 1957. They talked about values and arrays. COBOL (1960) introduced the record. C programmers know it as "struct". The record is heterogeneous. You talked about fields, not positions, within the collection. And these fields were named. struct employee { char name[200]; int yearBorn; double salary; int married; // Boolean char race; }; struct employee fulltimers[1000]; // array of structs fulltimers[0].yearBorn = 1957; fulltimers[1].salary = 100573.44; Of course to make the mini-database, you'll need an array of these records or structs. Python's dictionary is a generalization of a struct. In C, you associate a name (which is a string) to a value inside a struct (see above.) In Python, you can do the same (although the syntax is different, don't use the dot notation.) But in Python, the "name" can be any kind of object, including numbers, strings, sets, tuples, etc! Simula-67 (created by Ole-Johan Dahl and Kristen Nygaard in the 1960s in Oslo, Norway) was used for simulation. Introduced so many concepts that are in all our languages today. Amazing! It was the first object oriented programming lang. Think of a class as a struct with methods added to it. These methods often work on the fields of the struct: struct employee { char name[200]; int yearBorn; double salary; int married; // Boolean char race; void getRaise(double howMuchPct) { this.salary = this.salary + this.salary * howMuchPct; } }; Bjarne Stroupstrup created a "C with classes" in 1979 that did exactly this! He later renamed it to C++ because ++ means "add 1" or "go to the next level." Later C# was so named because it was C++++ or put two of the pluses underneath the others. C++ was wildly successful. Most modern OS are written in it! But it is complex and hard to learn. Smalltalk was another OOP system dating from 1980 at Xerox PARC where the mouse and WIMP interface (the GUI) was invented. Apple stole some of the ideas from Smalltalk and pasted them onto C and called it Objective C. That was what a lot of ios apps are written in. It has been replaced with Swift (2014). C. Object Oriented programming Part 2: nuts and bolts Read my Very quick tutorial (on D2L) starting with program 18 Modules and classes: what is the difference? a module is essentially just a file containing Python code. It may contain global variables, functions, classes. Modules cannot be nested. You do not make instances of modules. You import modules. Functions versus methods How to define a class How to make an instance of a class (i.e. an object) Ways that classes relate to each other: Inheritance (isa) Composition (hasa) Interaction (toucha) What is a pointer (a reference)? Functions and their parameters: call by value (for integers and strings and floats) call by reference (objects) Some examples: Point class: what are its members and methods? (pretty minimal, could convert to polar) Line: what are its members? what are its methods? (slope, distance between points) is the line finite or infinite? Defined by two points, or by a point and a slope? 2-way dictionary (1-to-1, not 1-to-many) Several different ways of implementing this (2 dicts, or 1 dict plus searching) RWOs = Real World Objects Examples: book, library, employee, department Clocks and calendars clocks—military time (24-hour) 12-hour clock Inheritance: In Python: class X(Y): In Java: class X extends Y; Class X is the subclass. Class Y is the superclass. Two ways of relating: 1. The subclass "decorates" or augments the superclass. It extends it, adds stuff to it. 2. The subclass is a special version of the superclass. It restricts or constricts it. The superclass is a more general version of it. Taxonomy of living things: http://en.wikipedia.org/wiki/Zoology_mnemonic Kingdom Phylum Class Order Family Genus Species All domestic cats are Felis catus. Felidae, all ar carnivora, mammalia, chordata, animalia It is more common to use the augmentation version, because in business you want to say for example that you have employees. Some are workers, some are managers, etc. Some part-time, some semi-retired, etc. Point3D (x,y,z) Point2D (x,y) is a special version of Point3D where z=0 always or You could let Point be the superclass, and then Point2D and Point3D come off that. box-shapes quadrilateral parallelogram rectangle square Containers: list, set, tuple, bag, limited bag, ordered set List: ordered, heterogeneous, not unique Set: unordered, hetero-, unique Bag: unordered, hetero-, not unique Limited Bag: like a Bag but there are limits on the number of instances (applies to all or a different limit for each value?) Ordered Set: ordered, hetero-, unique In some strongly typed prog. langs like Java or C, arrays are homogeneous. ArrayList is a kind of generic type. ArrayList<String> is a subclass of ArrayList with a limitation on it. We thought about a OneWayDict() G2E = OneWayDict() # German to English (and back) API get set del in lookup a word assign a new word pair or change existing word pair remove a word pair 2 functions a. __contains__ (if x in mylist: ) b. iterator (for x in mylist:) How can we implement this? Implement a class called Oracle.