Python Programming: List Comprehensions, Functions, and Classes in AI at USF - Prof. Chris, Study notes of Computer Science

This document from the university of san francisco's department of computer science introduces students to various python programming concepts, including list comprehensions, functions as objects, function arguments, introspection, classes, and inheritance. Students will learn how to use list comprehensions as a convenient way to map functions onto sequences, create higher-order functions using functions as objects, and define classes with instance variables and methods. The document also covers the use of optional and named function arguments and the concept of introspection, which allows python objects to be inspected for their attributes and methods.

Typology: Study notes

Pre 2010

Uploaded on 07/31/2009

koofers-user-h3c-1
koofers-user-h3c-1 🇺🇸

10 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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/??
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Python Programming: List Comprehensions, Functions, and Classes in AI at USF - Prof. Chris and more Study notes Computer Science in PDF only on Docsity!

Artificial Intelligence Programming

Objects in Python

Chris Brooks

Department of Computer ScienceUniversity of San Francisco

Department of 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^

’]]

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

3-4:^ Function Arguments^ •^ Python functions can have optional and named arguments^ def

example_fun(arg1,

arg2=’cats’,

arg3=’dogs’)

print^ arg1,

arg2,^

’ and^ ’,

arg

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.

Department of 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 list

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

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

Mammal^

... • This defines a class with no parent class.

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

3-8:^ init^ •^ __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/

3-10:^ Instance Variables^ •^ Instance variables

must^ be referenced with self

-^ 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/

3-11:^ 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.11/

3-13:^ Methods^ •^ built-in operators can be

overloaded

to provide polymorphism.

◦^ repr - controls how an object is printed. ◦^ lt, gt, le, gt, cmp : comparison operators ◦^ add, sub, mul, div : arithmetic operators

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

3-14:^ Class variables^ •^ Class variables are declared outside the scope of a method^ •^ 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.14/

3-16:^ Inheritance^ class^ Flyer

: "Class for^ things

that^ can

fly"

def^ init(self,

type=’’)

self.type=typedef canFly(self)

return^

True class^ Bird(Flyer)

def^ init(self,

type=’’)

Flyer.init()self.type

= type def^ sayName(self)

print^ "I

am^ a^ "

+^ self.type

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

3-17:^ 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.17/

3-19:^ Comments^ •^ 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() • this is called

introspection

(reflection in Java)

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