python 拾贝

1. 

 

内建的 type() 函数带三个参数时, 将作为强悍的动态类构造器. 如下:
 
type(name, bases, dict)

       返回一个新的type对象. 基本上是 class 语句的动态形式. 参数:

      name  , 字符串, 制定要构造类的名字, 赋给新对象的 __name__ 属性;

      bases,一个tuple,指定新类型的所有基类,赋给新对象的__bases__ 属性; 

      dict, 字典类型,作为新类的名字空间,赋给新对象的__dict__ 属性.

  

例如,下面两条语句作用完全一样:

  1. >>> class X(object):
  2. ... a = 1
  3. ...
  4. >>> X = type('X', (object,), dict(a=1))

注意:该特性需要 Python >= 2.2.

 


type(object)

带一个参数返回 object 的类型. 返回一个 type 对象. 建议用内建的 isinstance() 函数来确定对象类型.

 

 

2.   内建函数 int, 可以用来将字符串通过指定进制数对应的十进制数

  例如  :   int('11', 8)  => 9       int('11', 4) => 5,   int('A', 16) => 10

class int(object)

 |  int(x=0) -> int or long

 |  int(x, base=10) -> int or long

 |  

 |  Convert a number or string to an integer, or return 0 if no arguments

 |  are given.  If x is floating point, the conversion truncates towards zero.

 |  If x is outside the integer range, the function returns a long instead.

 |  

 |  If x is not a number or if base is given, then x must be a string or

 |  Unicode object representing an integer literal in the given base.  The

 |  literal can be preceded by '+' or '-' and be surrounded by whitespace.

 |  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to

 |  interpret the base from the string as an integer literal.

 |  >>> int('0b100', base=0)

 |  4

 

 

3.   字符  和    ASCII码 或  Unicode码  的相互转换

  ord('a')    =>    97              chr(97)   => 'a'      unichr(97)   => u'a'

  ord(u'郭')     =>  37101     unichr(37101)  =>  u'\u90ed'    (注: print u'\u90ed'    =>  郭     int('90ed',  16)   =>   37101

 

 

4.

>>> import ast

>>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")

{'muffin': 'lolz', 'foo': 'kitty'}

  

>>> for i,j in enumerate(range(0,10)):

...  print i,j

...

0 0

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

 

 

 

5.

Use of a single underscore in IDLE.

An underscore represents the result of a previous expression

 

>>> 1 + 2

3

>>> _ + 1

4

 

 

 

 6.

Circular lists :)

 

>>> def make_circular_list(a_list):

...   while True:

...     for an_item in a_list:

...       yield an_item

...

>>> a_list = [1,2,3]

>>> a_circular_list =  make_circular_list(a_list)

>>> a_circular_list.next()

1

>>> a_circular_list.next()

2

>>> a_circular_list.next()

3

>>> a_circular_list.next()

1

 

 7.

Finding the most frequent element in a list, x:

max(set(x), key=x.count)

 

 8.

编码错误解决方案:

(1).

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

ErrorMsg = ErrorMsg.decode('utf-8')

return HttpResponse(ErrorMsg)

(2).

看起来,你这里的问题,其实是:

把文件内容,写入到文件中时,出错了。

而出错的原因其实是,python系统,在使用默认的编码类型,此处的ascii,去将对应的内容,写入到文件中。

但是由于其中一些内容,ascii编码不支持,所以报错。

所以,更好的办法是,在输出的时候,对文件制定特定的UTF-8编码即可。

而无需改动默认编码。

具体做法是:

不使用open打开文件,而使用codecs:

fp = codecs.open(‘output.txt’, ‘a+’, ‘utf-8′);;

fp.write(row[1]);

fp.close();

 9.  版本号比较

from distutils.version import LooseVersion

LooseVersion(a) < LooseVersion(b)

 

10.  合并dict

python3

>>> x = {'a':1, 'b':2}
>>> y = {'c':3,'d':4}
>>> z = {**x, **y}
>>> z
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

 
posted @ 2014-09-05 10:58  flamedancer  阅读(174)  评论(0编辑  收藏  举报