ECS 36A Lecture Notes - Lecture 7: Iter, Chromosome, Init

63 views2 pages
6 Nov 2018
ECS 36A - Lecture 7 - Classes
if word in d:
d[word] += 1
d[word] = 1
~this requires checking the dictionary two times per iteration~
d[word] += 1
except keyError:
d[word] 1
~this is more efficient because for more common cases you only have to check the
dictionary once ~
Classes are much more powerful than functions:
Allows you to create objects and associate multiple functions and attributes to each
object within a class
Classes help to simplify programs and make them appear more organized
Useful when multiple a program needs multiple functions need access to be able to
change and alter the same variables
Will be useful in HW 3 & 4 of this course
class Human:
chromosome = 23
def __init__(self, name, gender): = name
self.gender = gender
def get_name(self):
print(Human.chromosome) prints 23
# print( # Error name is not an attribute
alice = Human('Alice', False)
bob = Human('Bob', True)
print(alice.chromosome,, alice.gender)
print(bob.chromosome,, bob.gender)
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.