码迷,mamicode.com
首页 > 编程语言 > 详细

<Python> Random Tricks

时间:2016-09-01 22:54:56      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/mjust/p/5831554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!