Docsity

Slides on Object Oriented Python - Artificial Intelligence Programming | CS 662, Study notes of Computer Science

Partial preview of the text

Download Slides on Object Oriented Python - Artificial Intelligence Programming | CS 662 and more Study notes Computer Science in PDF only on Docsity!

Artificial IntelligenceProgramming^ Object-oriented Python

Chris Brooks

Department of Computer ScienceUniversity of San Francisco

List Comprehensions Python has a great deal of support for

functional

programming^ This uses a built-in construct called a

list

comprehension List comprehensions are a convenient way to

map^ a

function onto a sequence. [x**2^ for^ x

in^ [1,2,3,4]] phoneBook

=^ {‘‘brooks’’:’’422-5221’’,

‘‘wolber’’:’’422-1234’’,’’parr’’:’’422-3435’’

[‘‘name: %s^ phone:

%’’^ %^ (nm,

ph)^ for

nm,^ ph^ in

phoneBook.items()][s.strip()

for^ s^ in

[’^ hello

’,^ ’^

in^ ’,^ ’^ there^

’]]

Department of Computer Science — University of San Francisco – p.1/

Building list comprehensions List comprehensions are not only neat, they’re alsomore efficient than traditional loops. Allows the interpreter to optimize the creation andassignment of iterators. Write your loop “inside out” Example: ###^ example:

filter^

a^ list^ of

words^ to

only^ find

those

###^ that^

have^ four

letters. final^ =^

[]

for^ word^

in^ wordlist:if len(word)^

==^ 4:

final.append(word)

Department of Computer Science — University of San Francisco – p.3/

Building list comprehensions What do we produce as a result?^ The word. [word ... ] What are we iterating over?^ wordlist. [word for word in wordlist ... ] When do we want to produce the word?^ When it has 4 letters. [word for word in wordlist iflen(word) == 4]

Department of Computer Science — University of San Francisco – p.4/

Functions as Objects In Python, functions are first-class objects^ Can be assigned to variables, passed as arguments,evaluated.^ This allows us to create higher-order functions def cube(x)

: return

x^ * x

* x

def my_map(list,

fn) :

list^

= []

for item

in^ list

list2.append(fn(item))return^ list

Department of Computer Science — University of San Francisco – p.6/

Introspection All Python objects are capable of introspection^ This may look familiar to you if you’ve used Java’sreflection API type(obj) dir(obj) obj.doc

Department of Computer Science — University of San Francisco – p.7/

init __init__is the first method called when an object iscreated.^ Technically, it’s not a constructor, but close enough Takes at least one argument: self This is a pointer to the object

Department of Computer Science — University of San Francisco – p.9/

init class Mammal^ : "Documentation

for^ the mammal^

class"

def^ init(self,

type=’’)

self.type

=^ type

Notice def is indented one level Notice init can use default arguments. (this is how toimplement multiple ’constructors’)

Department of Computer Science — University of San Francisco – p.10/

Methods Defined with def Inside the scope of the class definition. self is first argument in declaration^ Not provided when calling the method - the Pythoninterpreter fills this in. Can use default and keyword arguments

Department of Computer Science — University of San Francisco – p.12/

Methods class Mammal^ : "Documentation

for^ the mammal^

class"

def^ init(self,

type=’’)

self.type

=^ type def^ sayName(self)

print^ ’I am^ a^ ’, self.type ###^ this is^ a^ builtin

method^ that^ controls

how^ an^ object^ is

printed

###^ return

the^ string

indicating

how^ the object^

should^ be

displayed

def^ repr(self)

return^ self.type

Department of Computer Science — University of San Francisco – p.13/

Class variables Class variables are declared outside the scope of amethod Referenced with the class name Useful for constant values, counters,mutex/semaphores, etc. Places where you don’t want to create an object^ string.letters, string.ascii_lowercase, etc

Department of Computer Science — University of San Francisco – p.15/

Inheritance Like all OO languages, Python supports inheritance. Include the name of the parent class in parentheses. To call a parent class’ method, use the name of theparent class.

Department of Computer Science — University of San Francisco – p.16/

Multiple Inheritance Python also supports multiple inheritance List multiple classes in declaration Names are resolved from left to right

Department of Computer Science — University of San Francisco – p.18/

Multiple Inheritance class Bat^ (Mammal,

Flyer)^

def^ init(self)

self.type

=^ ’bat’ def^ sayName(self)

Mammal.sayName(self)print^ "I am^ not^ a^ vampire,

though"

Department of Computer Science — University of San Francisco – p.19/