Andy Niu �����ĵ�

Andy Niu

Andy Niu Help  1.0.0.0
Python常用功能

变量

 批量重命名文件
 
 Python之道
 
 Python命名空间
 

详细描述

变量说明

Python之道
import this
The Zen of Python, by Tim Peters
Python之道
Beautiful is better than ugly.
美观胜于丑陋。
Explicit is better than implicit.
显示胜于隐式。
Simple is better than complex.
简单胜于复杂。
Complex is better than complicated.
复杂胜于过度复杂。
Flat is better than nested.
平面胜于嵌套。
Sparse is better than dense.
稀少胜于稠密。
Readability counts.
可读性需要考虑。
Special cases aren't special enough to break the rules.
即使情况特殊,也不应打破原则,
Although practicality beats purity.
尽管实用胜于纯净。
Errors should never pass silently.
错误不应悄无声息的通过,
Unless explicitly silenced.
除非特意这么做。
In the face of ambiguity, refuse the temptation to guess.
当有混淆时,拒绝猜测(深入的搞明白问题)。
There should be one-- and preferably only one --obvious way to do it.
总有一个,且(理想情况下)只有一个,明显的方法来处理问题。
Although that way may not be obvious at first unless you're Dutch.
尽管那个方法可能并不明显,除非你是荷兰人。(Python的作者Guido是荷兰人,这是在致敬)
Now is better than never.
现在开始胜过永远不开始,
Although never is often better than *right* now.
尽管永远不开始经常比仓促立即开始好。
If the implementation is hard to explain, it's a bad idea.
如果程序实现很难解释,那么它是个坏主意。
If the implementation is easy to explain, it may be a good idea.
如果程序实现很容易解释,那么它可能是个好主意。
Namespaces are one honking great idea -- let's do more of those!
命名空间是个绝好的主意,让我们多利用它。

"Python之道"强调美观、简单、可读和实用,拒绝复杂或模糊。
Python命名空间
1、python彻底分离了变量名称和变量值,名称可以指向任何值。
2、定义一个名称的时候,必须赋值,否则报错:
    >>> a
    
    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
        a
    NameError: name 'a' is not defined
    原因是:没有对a进行赋值,python解释器认为是引用a,但是在命名空间内找不到a
    也就是说,定义一个名称的时候,没有赋值,语义就变了,不是定义,而是引用一个名称。
3、赋值的时候,也就是定义了一个名称。这一点特别重要。如下:
    i=1
    def func():
        i=i+1
    
    func();
    报错如下:
    Traceback (most recent call last):
    File "D:\Temp\NiuzbRepository\trunk\Python\自学教程\namespace\a.py", line 5, in <module>
        func();
    File "D:\Temp\NiuzbRepository\trunk\Python\自学教程\namespace\a.py", line 3, in func
        i=i+1
    UnboundLocalError: local variable 'i' referenced before assignment
    原因是:进入方法func内,i=i+1是赋值,相当于在局部定义了一个名称i,
    这个i不是全局的i,而是新定义了一个名称i,但是在赋值的右边,这个i先被引用了。
    怎么解决这个问题?
    就是在赋值之前,不要定义名称i,而是从全局引用i,如下:
    i=1
    def func():
        global i
        i=i+1
        print i
    
    func();
    print i
4、特别注意:下面表示的不同语义:
    i=100   // 定义一个名称
    i       // 引用一个名称
5、命名空间是一个 字典(dictionary) ,它的键就是变量名,它的值就是那些变量的值。
6、一个名称的顺序是 LEGB,如下:
    L(local)方法
    E(enclosing function) 嵌套方法
    G(global) 全局,也就是一个模块
    B(builtin)内置命名空间
7、命名空间的访问
    locals();
    globals();
批量重命名文件
考虑下面的需求,对于目录下,dmu1.h,dmu1.cpp,dmu2.h,dmu2.cpp,... 
重命名为vms1.h,vms1.cpp,vms2.h,vms2.cpp,...,代码如下:
import os
target_path="D:/test"
print target_path
fileList = os.listdir(target_path)
print fileList

for a in fileList:
    postfix=a.split(".")[1]
    
    if postfix == "h" or postfix == "cpp":        
        os.rename(target_path+"/"+a,target_path+"/"+a.replace("dmu","vms"))
Copyright (c) 2015~2016, Andy Niu @All rights reserved. By Andy Niu Edit.