Python Flashcards

1
Q

Iterable protocol

A

iterables (implementing __iter__() )
iterators (implementing __next__() )
can be used in
for [var] in [iterable].

Functionaly equivalent to:
obj = train.\_\_iter\_\_()
name = obj.\_\_next\_\_()
do something with name
name = obj.\_\_next\_\_()
do something with name
... until IndexError is raised (?)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

‘with’ protocol

A

__enter__()

__exit__()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Dictionary comprehension on prev dict

A
new_dict = {key:value for (key,value) in old_dict.items()}
new_dict = {key:value for (key,value) in old_dict.items()}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Dictionary comprehension from scratch

A

{n:n**2 for n in numbers if n%2 == 0}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Dictionary comprehension conditional val

A

{k:(‘even’ if v%2==0 else ‘odd’) for (k,v) in dict1.items()}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Dictionary creation

A

dict_var = {key1:val1, key2:val2}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Set creation

A

set_var = {val1, val2}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List creation

A

list_var = [ val1, val2 ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Set comprehension

A

{s**2 for s in range(10)}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List comprehension

A

[s**2 for s in range(10)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Lists vs Tuples

A

lists are for looping; tuples for structs. Lists are homogeneous; tuples heterogeneous. Lists for variable length.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

__name__ contents

A

“__main__” - if you run this script,

filename - if you import the script as a module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Override comparison

A
>>> class Whateva:
...     def \_\_eq\_\_(self, other):
...         return True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

REPL

A

== Interpreter.

Read, Evaluate, Print, Repeat.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Equality vs Identity

A
'==' vs 'is'
contents equal (and overridable) , vs references the same object (non overridable)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s mutable?

A

List, Dictionary, class: Mutable

Tuple, string, number: Immutable

17
Q

Test membership

A

> > > 3 not in [2, 3, 4]
False
(2, 3) in [(2, 3), (5, 6), (9, 1)]
True

18
Q

Override arithmetics

A
x + y invokes x.\_\_add\_\_(y)
x - y invokes x.\_\_sub\_\_(y)
x * y invokes x.\_\_mul\_\_(y)
x / y invokes x.\_\_truediv\_\_(y)
x ** y invokes x.\_\_pow\_\_(y)
19
Q

Overriding container methods

A

len(x) invokes x.__len__()
x[key] invokes x.__getitem__(key)
x[key] = item invokes x.__setitem__(key, item)
item in x invokes x.__contains__(item)
iter(x) invokes x.__iter__()
next(x) invokes x.__next__()

20
Q

Override print behavior

A

__str__ calls __repr__, override either one.

21
Q

self.__class__, self.__dict__

A

? TODO

22
Q

__eval__

A

? TODO

23
Q

ctor

A

__init__

24
Q

Brackets operator ()

A

__call__