5 Python Fundamentals m04 Objects Slides
5 Python Fundamentals m04 Objects Slides
Objects
x = 1000
x = 1000
int
x 1000
x = 500
int
x 1000
int
500
y=x
y
x
int
500
x = 3000
x y
int int
3000 500
returns a unique identifier for an object
t
int
5
t
5 + 2 = 7
t
5 2 7
int
t 7
list
r [2, 4, 6]
s
list
r [2, 4, 6]
id() deals with the object, not
the reference
list
[2, 4, 6]
Variables
Named references to objects
name object
list
p [4, 7, 11]
list
q [4, 7, 11]
Value equality vs. identity
k
list
m [9, 15, 24]
modify(k):
k
list
m [9, 15, 24, 39]
modify(k):
k
list
m [9, 15, 24, 39]
list
f [14, 23, 37]
list
f [14, 23, 37]
replace(g):
g
list
f [14, 23, 37]
list
g
list
f [14, 23, 37]
list
g
list
f [14, 23, 37]
Pass By Object Reference
The value of
ref object
the reference
function(arg): is copied, not
the value of
arg the object.
Default Arguments
[]
def add_spam(menu=
):
list
[“spam”]
def add_spam(menu=
):
list
[“spam”,
“spam”]
def add_spam(menu=
):
list
[“spam”,
“spam”,
“spam”,]
Static Dynamic
Haskell
Strong
C++
Weak
Static Dynamic
Haskell
Strong
C++
Weak
Dynamic Type System
name object
Python Name Scopes
Local
Enclosing
Global
Built-in
#!/usr/bin/env python3 def main(url):
"""Retrieve and print words from a URL. """Print each word from a text document from at a URL.
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def print_items(items):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for item in items:
print(item)
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
def main (url):
Tools to read a UTF-8 text document from a URL which """Print each word from a text document from at a URL.
will be split into its component words for printing.
Args:
Script usage: url: The URL of a UTF-8 text document.
"""
python3 words.py <URL> words = fetch_words(url)
""" print_items(words)
import sys
from urllib.request import urlopen if __name__ == '__main__':
main(sys.argv[1]) # The 0th arg is the module filename.
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def print_items(items):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for item in items:
print(item)
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
def main (url):
Tools to read a UTF-8 text document from a URL which """Print each word from a text document from at a URL.
will be split into its component words for printing.
Args:
Script usage: url: The URL of a UTF-8 text document.
"""
python3 words.py <URL> words = fetch_words(url)
""" print_items(words)
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def print_items(items):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for item in items:
print(item)
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
def main (url):
Tools to read a UTF-8 text document from a URL which """Print each word from a text document from at a URL.
will be split into its component words for printing.
Args:
Script usage: url: The URL of a UTF-8 text document.
"""
python3 words.py <URL> words = fetch_words(url)
""" print_items(words)
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def print_items(items):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for item in items:
print(item)
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
def main (url):
Tools to read a UTF-8 text document from a URL which """Print each word from a text document from at a URL.
will be split into its component words for printing.
Args:
Script usage: url: The URL of a UTF-8 text document.
"""
python3 words.py <URL> words = fetch_words(url)
""" print_items(words)
from urllib.request import urlopen main(sys.argv[1]) # The 0th arg is the module filename.
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def print_items(items):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for item in items:
print(item)
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
def main (url):
Tools to read a UTF-8 text document from a URL which """Print each word from a text document from at a URL.
will be split into its component words for printing.
Args:
Script usage: url: The URL of a UTF-8 text document.
"""
python3 words.py <URL> words = fetch_words(url)
""" print_items(words)
from urllib.request import urlopen main(sys.argv[1]) # The 0th arg is the module filename.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def print_items(items):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for item in items:
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
def main (url):
Tools to read a UTF-8 text document from a URL which """Print each word from a text document from at a URL.
will be split into its component words for printing.
Args:
Script usage: url: The URL of a UTF-8 text document.
"""
python3 words.py <URL> words = fetch_words(url)
""" print_items(words)
from urllib.request import urlopen main(sys.argv[1]) # The 0th arg is the module filename.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
Args:
An iterable series of printable items.
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
word
for in line_words:
story_words.append(word)
print(locals())
return story_words
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
forline in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def fetch_words(url ):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
def fetch_words(url ):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from
the document.
"""
story:
with urlopen(url) as
story_words = []
for line in story:
line_words = line.decode('utf8').split()
for word in line_words:
story_words.append(word)
print(locals())
return story_words
rebinds a global name at module scope
"""Demonstrate scoping."""
count = 0
def show_count():
print("count = ", count)
def set_count(c):
count = c
"""Demonstrate scoping."""
count = 0
def show_count():
print("count = ", count)
def set_count(c):
global count
count = c
Moment of Zen