Unpacking

1
2
3
4
5
6
7
8
9
10
for index, item in enumerate(some_list):
# do something with index and item

a, b = b, a
a, (b, c) = 1, (2, 3)
# in Python 3:
a, *rest = [1, 2, 3]
# a = 1, rest = [2, 3]
a, *middle, c = [1, 2, 3, 4]
# a = 1, middle = [2, 3], c = 4
阅读全文 »

Definition

A context manager is a python object that provides extra contextual information
to an action. Context managers are a way of allocating and releasing some sort of resources exactly where you need it.

Example

The most well known example is file access:

1
2
with open('file.txt') as f:
contents = f.read()

f’s close method will be called at some point.

阅读全文 »

Social Thinking

  1. Sometimes people will even self-handicap with self-defeating behaviors that protect self-esteem by providing excuses for failure
  2. Life’s greatest achievements, but also its greatest disappoinments, are born of the highest expectations.
  3. No single truth is ever sufficient, because the world is complex.
  4. Spontaneous trait inference : when we say something good or bad about another, people spontaneously tend to associate that trait with us. So it’s better not to say something bad about another.
    阅读全文 »

Basic

package import

If the import module in the same dir

1
from . import core

If the import module in the top dir

1
from .. import core

阅读全文 »