Intermediate Level
Question | Answer |
---|---|
01.Create simple class | collections.namedtuple (see code) |
02.__str__ VS __repr__ |
str() calls the latter when the former is missing. |
03.Why exclude the last item when sclicing | One reason is lst[:n] + lst[n:] == lst |
04.Variables created inside listcomp | They exist after listcomp |
05.Application of score to grade | bisect.bisect (see code) |
06.Counterpart of Map::computeIfAbsent |
dict.setdefault |
07.Tricky way to handle missing key for a dict | __getitem__ fallback to __missing__ . So implement the latter accordingly. |
08.Check the existing of an attribute in instance method | hasattr(self, 'xxx') |
09.Closure and nested function | Local variables in outer function are free variables for inner function. When inner function returns as return value, free variables captured as closure. |
10.Local variables in the current scope | locals() built-in function to return a dict |
11.is operator VS == | is is faster in that it compares id, while == could be overrided in user type with __eq__ |
12.Mutable object used as default value of function parameter | Default value evaluated once when function is defined. So the instance of default value is shared among methods from different instances and therefore is dangerous. |
13.Things can keep unexpected references so as to prevent objects from destroying | _ and Traceback object. |
14.Quickly compare multiple variables | tuple(this) == tuple(other) |
15.Another way of hash code generation other than odd prime 31 and 17 | xor |
1 | # 01 |
reference
Fluent Python 2015.7