


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



?? Department of Computer Science — University of San Francisco – p.1/
[1,2,3,4]] x in for [x**
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/
"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/
: 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
?? Department of Computer Science — University of San Francisco – p.4/
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/