摘要: 闭包:对闭包的具体定义有很多种说法,这些说法大体可以分为两类:◆一种说法认为闭包是符合一定条件的函数,比如参考资源中这样定义闭包:闭包是在其词法上下文中引用了自由变量的函数。◆另一种说法认为闭包是由函数和与其相关的引用环境组合而成的实体。比如参考资源中就有这样的的定义:在实现深约束时,需要创建一个能显式表示引用环境的东西,并将它与相关的子程序捆绑在一起,这样捆绑起来的整体被称为闭包。python中闭包 表示内层函数 由一个变量指代 而这个变量对于外层包含的函数而言是本地变量 看一个例子:def make_adder(addend): def adder(augend): ... 阅读全文
posted @ 2012-07-21 15:36 cacique 阅读(1025) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# -*- coding: utf-8 -*-#简化字符串的translate 方法的使用import stringdef translator(frm='',to='',delete='',keep=None): if len(to) == 1: to = to * len(frm) trans = string.maketrans(frm,to) if keep is not None: allchars = string.maketrans('','') delete = a 阅读全文
posted @ 2012-07-21 15:18 cacique 阅读(595) 评论(0) 推荐(0) 编辑
摘要: 最简单的方法如下def containsAny(seq,aset): """ 检查序列seq是否含有aset中的项""" for c in seq: if c in aset: return True return False检查序列seq中是否含有aset中所有的项def containsAll(seq,aset): return not set(aset).difference(seq)对于任何set对象a a.difference(b) 结果为a-set(b) 返回啊中所有不属于b的元素 阅读全文
posted @ 2012-07-19 22:01 cacique 阅读(193) 评论(0) 推荐(0) 编辑
摘要: 反转字符最简单的方法是使用步长为-1的方法revchars = astring[::-1]反转字符串 应先创建一个单词列表 再将列表反转 在用join方法合并rewords = astring.split()rewords.reverse()rewords = ' '.join(rewords)#也可以用一行代码表示rewords = ' '.join(astring.split()[::-1])逐词反转还保留原来空格的话import rerevwords = re.split(r'(\s+)',astring)revwords.reverse( 阅读全文
posted @ 2012-07-19 21:52 cacique 阅读(307) 评论(0) 推荐(0) 编辑
摘要: 配置數據庫 1 DATABASES = { 2 'default': { 3 # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 4 'ENGINE': 'django.db.backends.mysql', 5 'NAME': 'django', # Or path to database file if using sqlite3. 6 'USER': ' 阅读全文
posted @ 2012-03-28 21:35 cacique 阅读(883) 评论(0) 推荐(1) 编辑
摘要: dot filesdot files是指以.开头的文件,比如说.bash_profile。在类Unix系统下,dot files是默认隐藏的。在Shell下,很多的工具使用dot file作为默认导入的配置文件。比如说Bash对应的.bash_profile和.bashrc。在Shell下查看这些文件需要用ls -a。在你的home directory下用这个命令,你可能会看到一些已经存在的dot files。.bash_profile & .bashrc这两个文件的区别:.bash_profile 是以交互式、login方式进入bash时会调用的.bashrc 是交互式、non-lo 阅读全文
posted @ 2012-03-25 20:41 cacique 阅读(3174) 评论(0) 推荐(0) 编辑
摘要: 首先贴出答案问题 :找不到libmysqlclient.18.dylib,把mysql安装目录的这个文件link到/usr/lib下:sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/安装过程第一次 问题ImportError:dlopen(/Users/aj/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-ix86_64.egg-tmp/_mysql.so,2):no suitable image found.Did find:/Users/aj/.pytho. 阅读全文
posted @ 2012-03-25 18:23 cacique 阅读(3786) 评论(0) 推荐(1) 编辑
摘要: 在终端使用模板from django import templatet = template.Template('My name is {{ name }}.')提示错误Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Python/2.7/site-packages/django/template/base.py", line 123, in __init__ if settings.TEMP 阅读全文
posted @ 2012-03-15 21:54 cacique 阅读(834) 评论(0) 推荐(0) 编辑
摘要: 先看一个例子 #coding:utf-8#Filename:using_flie.py'''Created on 2012-2-27@author: goodspeedcheng'''poem = '''\Programming is funWhen the work is doneif you wanna make your work also fun: use Pytho... 阅读全文
posted @ 2012-02-27 18:42 cacique 阅读(1028) 评论(0) 推荐(0) 编辑
摘要: 生成器是一次生成一个值的特殊类型函数。可以将其视为可恢复函数。调用该函数将返回一个可用于生成连续 x 值的生成器【Generator】 简单的说就是在函数的执行过程中,yield语句会把你需要的值返回给调用生成器的地方,然后退出函数,下一次调用生成器函数的时候又从上次中断的地方开始执行,而生成器内的所有变量参数都会被保存下来供下一次使用。 示例代码 >>> def fib(max): ... 阅读全文
posted @ 2012-02-24 20:47 cacique 阅读(7633) 评论(0) 推荐(1) 编辑
Copyright ©2011 Goodspeed Cheng