chaeny 2019. 7. 26. 03:49

Interfaces

Describe how you can interact with an object without necessarily implementing it

Magic Methods

These are specially named methods that are callable outside the ordinary dot notation.

Example: __init__

str & repr

str

__str__ specifies what occurs when str is called on it

Used when calling print on an object

repr

__repr__ specifies what occurs when repr is called on it

Used when displaying an object in the interpreter

Enhancing Linked Lists

repr for Linked Lists

>>> lnk = Link(1, Link(2, Link.empty))

>>> lnk

Link(1, Link(2))

str for Linked Lists

>>> lnk = Link(1, Link(2, Link.empty))

>>> print(lnk)

<1, 2>

equality

Sequences

x in s , x not in s : Returns boolean representing membership

s + t : Concatenates two sequences

s * n, n * s : Concatenates n shallow copies

len(s) : Number of elements in sequence

s[i] : i’th item of sequence

s[i:j], s[i:j:k] : Slicing

membership : x is s, x not in s

concatenation : s + t

repetition : s * n, n * s

length : len(s)

indexing : s[i]

slicing : s[i:j], s[i:j:k]