页面

显示标签为“python”的博文。显示所有博文
显示标签为“python”的博文。显示所有博文

2010-03-13

Python名字绑定

对复合类型,记住一点:同是等号(“=”),赋值是赋值,计算是计算。赋值把一个对象(同一块内存)绑定到多个名字;计算是把一个名字绑定的对象复制一下,然后绑定到其它名字。

例如,现在有个序列m = [0, 1, 2],下面这个是赋值:
k = m
k.remove(2)后:m = [0, 1]。下面这个是计算:
k = list(m) # 或
k = m[ : ]
k.remove(2)后m不变化:m = [0, 1, 2]
对字符串、数字等基本的、不可变类型,每次赋值都是创建副本的操作:
a = 0
b = a
执行a = 1后:b = 0。

2009-05-16

绚丽的python界面框架

pysta,一个很华丽的python界面设计框架:
http://www.codeplex.com/pysta

2009-02-23

compile python program

Compile Python programs in one directory with module compileall:
$ python -mcompileall a_directory

2008-11-17

Python Float Number Round and Format

A useful function that can round a float's significant digit(s):
def round_to_n(x, n):
"""
Rounds float x to n significant digits, in float notation (i.e. 'f', other notation: e (scientific notation), g (when digits is 6 longer, use seientific notation, otherwise float notation)).
"""
    if n < 1:
        raise ValueError("number of significant digits must be >= 1")
    return "%.*f" % (n-1, round(x, n))