Mutability date Obj in Python String 1 2 s = "Hello" s.swapcase() # "hELLO" ASCII 和表格对应 0x41 –> row 4, col 1 😮
1 2 3 4 5 from unicodedata import name, lookup name('A') # 'LATIN CAPITAL LETTER A' lookup('LATIN CAPITAL LETTER A') # 'A' lookup('SNOWMAN') # '☃' lookup('FACE WITH TEARS OF JOY').encode('utf-8') # '😂'.encode('utf-8') Mutation operations Mutable objects List Dictionary Set? 1 2 3 4 5 6 # List, pop, remove, append, extend lst = [1, 2, 3] lst.
Trees A tree has a root label and a list of branches and each branch is a tree itself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 def tree(label, branches=[]): for branch in branches: assert is_tree(branch), 'branches must be trees' return [label] + list(branches) # make sure branches is a list def label(tree): return tree[0] def branches(tree): return tree[1:] def is_tree(tree): if type(tree)!
Data Representation what is data???
从对象的行为(函数与方法)来定义
Example: Rational Numbers 纯粹使用函数来做接口而不是内在设置列表存储 🤔
data representation – a methodology constructor & selector thoughts
感觉这里更像是api设计的思路,哲学思想
仔细思考用好 constructor 和 selector 的作用 !
有没有一种可能,java没这么多事
Pairs 和 __getitem__ ?
1 2 3 from operator import getitem getitem(pair, 0) # first element getitem(pair, 1) # second element 1 2 3 from fractions import gcd ### gcd is used to find the greatest common divisor of two numbers gcd(pair[0], pair[1]) # greatest common divisor