首页 > 代码库 > <Python> Random Tricks

<Python> Random Tricks

1. with-as (context-manager)

 

with function() as instance:

  #do something

 

Codes above do the following:

Let instance = function(), where function returns an instantiation of some object, and this object has defined method __enter__() and __exit__().

In addition, before do something, execute __enter__(),

and after do something, execute __exit__() which are defined in the class of instance.

 

For detail, see http://blog.csdn.net/suwei19870312/article/details/23258495

 

 

2. basic data type manipulation

example:

[1] + [2] -> [1, 2]

[1] * 3 = [1, 1, 1] (or written as [1 for _ in range(3)])

#to be supplemented

<Python> Random Tricks