Docsity

Objects in Python - Artificial Intelligence Programming | CS 662, Study notes of Computer Science

Toggle sidebar
bg1
Artificial Intelligence Programming
Objects in Python
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science —University of San Francisco – p.1/??
3-2: List Comprehensions
•Python has a great deal of support for functional programming
◦Can do it by hand using map, filter, and lambda.
◦Also 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 ’]]
Departmentof Computer Science —University of San Francisco – p.2/??
3-3: List comprehensions
•Some cool list comprehension tricks:
params = {"server":"mpilgrim", "database":"master", "uid":"sa",
"pwd":"secret"}
[k for k, v in params.items()]
[(v,k) for k,v in params.items()]
["%s=%s" % (k, v) for k, v in params.items()]
[x + y / 2 for (x,y) in zip([1,2,3,4], [5,6,7,8])]
thenewlist = [x for x in theoldlist if x > 5]
Departmentof Computer Science —University of San Francisco – p.3/??
3-4: Function Arguments
•Python functions can have optional and named arguments
def example_fun(arg1, arg2=’cats’, arg3=’dogs’) :
print arg1, arg2, ’ and ’, arg3
example_fun(’I love’)
I love cats and dogs
example_fun("I love", arg2=’fish’)
I love fish and dogs
example_fun(’I love’, ’fish’, ’chips’)
I love fish and chips
example_fun(’I love’, arg3=’birds’)
I love cats and birds
•This makes it easy to implement multiple versions of a function.
Departmentof Computer Science —University of San Francisco – p.4/??
3-5: 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) :
list2 = []
for item in list :
list2.append(fn(item))
return list2
Departmentof Computer Science —University of San Francisco – p.5/??
3-6: Introspection
•All Python objects are capable of introspection
◦This may look familiar to you if you’ve used Java’s reflection
API
•type(obj)
•dir(obj)
•obj.__doc__
Departmentof Computer Science —University of San Francisco – p.6/??
pf3
pf4

Partial preview of the text

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

Artificial Intelligence Programming

Objects in Python

Chris Brooks

Department of Computer Science

University of San Francisco

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

List Comprehensions 3-2:

functional programming Python has a great deal of support for •

Can do it by hand using map, filter, and lambda. ◦

list comprehension Also a built-in construct called a ◦

a map List comprehensions are a convenient way to ◦

function onto a sequence.

[1,2,3,4]] x in for [x**

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

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

in ph nm, for ph) (nm, % %’’ phone: %s [‘‘name: phoneBook.items()] ’]] there ’ ’, in ’ ’, hello [’ s in for [s.strip() ?? Department of Computer Science — University of San Francisco – p.2/

List comprehensions 3-3:

Some cool list comprehension tricks: •

"uid":"sa", "database":"master", {"server":"mpilgrim", = params "pwd":"secret"} params.items()] in v k, for [k params.items()] in k,v for [(v,k) params.items()] in v k, for v) (k, % ["%s=%s" [5,6,7,8])] zip([1,2,3,4], in (x,y) for 2 / y + [x 5] > x if theoldlist in x for = [x thenewlist ?? Department of Computer Science — University of San Francisco – p.3/

Function Arguments 3-4:

Python functions can have optional and named arguments •

: arg3=’dogs’) arg2=’cats’, example_fun(arg1, def arg3 ’, ’ and arg2, arg1, print love’) example_fun(’I dogs and cats love I arg2=’fish’) love", example_fun("I dogs and fish love I ’chips’) ’fish’, love’, example_fun(’I chips and fish love I arg3=’birds’) love’, example_fun(’I birds and cats love I

This makes it easy to implement multiple versions of a function. •

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

Functions as Objects 3-5:

In Python, functions are first-class objects •

Can be assigned to variables, passed as arguments, ◦

evaluated.

This allows us to create higher-order functions ◦

x x * * x return cube(x) : def

: my_map(list, fn) def

[] list2 =

: list item in for

list2.append(fn(item))

return list

Introspection 3-6:

All Python objects are capable of introspection •

This may look familiar to you if you’ve used Java’s reflection ◦

API

type(obj) •

dir(obj) •

obj.doc •

Python Classes 3-7: Classes are defined with the ’class’ keyword: • : Mammal class

This defines a class with no parent class. • ?? Department of Computer Science — University of San Francisco – p.7/ init 3-8: __init__is the first method called when an object is created. • 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.8/ init 3-9: : Mammal class class" mammal the for "Documentation : type=’’) init(self, def = type self.type Notice def is indented one level • Notice init can use default arguments. (this is how to implement • multiple ’constructors’) ?? Department of Computer Science — University of San Francisco – p.9/ Instance Variables 3-10: be referenced with self must Instance variables • Created when a value is assigned to them • No separate class definition/header file ◦ This can lead to confusion ◦ Convention: set up all instance variables inside init ◦ Data members are public • ?? Department of Computer Science — University of San Francisco – p.10/ Methods 3-11: Defined with def • Inside the scope of the class definition. • self is first argument in declaration • Not provided when calling the method - the Python ◦ interpreter fills this in. Can use default and keyword arguments • Methods 3-12: : Mammal class class" mammal the for "Documentation : type=’’) init(self, def = type self.type : sayName(self) def self.type ’, a am ’I print printed is object an how controls that method builtin a is this ### displayed be should object the how indicating string the return ### : repr(self) def self.type return

Comments 3-19: Always need to use self to refer to member variables • All variables are public • can use __spam to indicate private variables ◦ Classes can tell you what methods they implement with dir() • (reflection in Java) introspection this is called • ?? Department of Computer Science — University of San Francisco – p.19/