Home CS61A
Post
Cancel

CS61A

currently working on lab07

CS61A Week 6

  • objects have attributes today.year and method today.strftime(...)
  • objects with good design will do what it supposed to do str(tmr - today)
  • objects contain built in informations today.strftime(%A %B %d). We didn’t pass in any vocabulary in the constructor.
>>> from datetime import date
>>> date
<class 'datetime.date'>
>>> today = date(2022, 8, 24)
>>> tmr = date(2022, 8, 25)
>>> str(tmr - today)
'1 day, 0:00:00'
>>> today.year
2022
>>> today.month
8
>>> today.strftime('%A %B %d')
'Wednesday August 24'

string is also object

>>> s = 'Hello'
>>> s.upper()
'HELLO'
>>> s.lower()
'hello'
>>> s.swapcase()
'hELLO'
>>> a = 'A'
>>> ord(a)
65
>>> hex(ord(a))
'0x41'
>>> from unicodedata import name, lookup
>>> name('A')
'LATIN CAPITAL LETTER A'
>>> name('a')
'LATIN SMALL LETTER A'
>>> lookup('SNOWMAN')
'☃'
>>> lookup('BABY')
'👶'
>>> lookup('BABY').encode()
b'\xf0\x9f\x91\xb6'

Lists Are Mutable Objectes

  • when assign an object to a name, name is just a alias..
  • all the different names point to the same object
  • the differences between == and is
>>> a = [1, 2, 3]
>>> b = a
>>> b
[1, 2, 3]
>>> b.append(4)
>>> b
[1, 2, 3, 4]
>>> a
[1, 2, 3, 4]
  • if a list is passed to a function through argument, the function can modify the list.
  • even if it is not passed to the argument, function can still change the list
a = [1, 2, 3]
func(a)
 # now a = [1, 2, 3, 4]
g()
 # now a = [1, 2, 3, 4, 5]
  • a very dangerous and confusing desgin is the default value for argument
>>> def f(s=[]):
...     s.append(1)
...     return s
...
>>> f()
[1]
>>> f()
[1, 1]
>>> f()
[1, 1, 1]
  • When append, extend, addition, slicing lists, it can be very confusing which generate new lists. Be careful!

Tuples Are Immutable Objects

  • objects such as list and dict are mutable objects.
  • tuple is similar to list, but it not mutable objects, it is not allowed to make new assignments to the tuple
  • but it is not saying it’s impossible to change tuple
a = ([1, 2], 3)
 # a[0] = 4    # NOT ALLOWED
 # it is not allowed to assign to a[0]
 # but you can do it on a[0][0]
a[0][0] = 4 # now a = ([4, 2], 3)

Iterator

lst = [1, 2, 3]
t = iter(lst)
next(t)

or iterator for dict

t = iter(d.keys())  # or iter(d)
t = iter(d.values())
t = iter(d.items())

Built-in functions for iteration: many built-in Python sequence operations return iterators that compute results lazily

map(func, iterable)
filter(func, iterable)
zip(first_iter, second_iter)
reversed(sequence)
list(iterable)
tuple(iterable)
sorted(iterable)
This post is licensed under CC BY 4.0 by the author.