随笔分类 -  python

摘要:copy— Shallow and deep copy operations Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module p. 阅读全文
posted @ 2013-05-09 10:11 tangr206 阅读(301) 评论(0) 推荐(0)
摘要:之前在讨论闭包的时候有提到: Python会按LEGB的顺序来搜索变量:要说明的是,这里的访问规则只对普通变量有效, 对象属性的规则与这无关(简单地说,访问一个对象的属性与此无关)。L. Local. 局部作用域,即函数中定义的变量(没有用global声明)E. Enclosing. 嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部作用域,比如上面的示例中的labmda所访问的x就在其父级函数test的局部作用域里。通常也叫non-local作用域。G. Global(module). 在模块级别定义的全局变量(如果需要在函数内修改它,需要用global声明)B. Built-... 阅读全文
posted @ 2013-05-07 16:28 tangr206 阅读(1322) 评论(0) 推荐(0)
摘要:区别Being educated under Java background, static method and class method are the same thing.But not so in Python, there is subtle difference:Sayfunction a()is defined inParentClass, whileSubClass extendsParentClassIf functiona()has@staticmethoddecorator,Sub.a()still refers to definition insideParentCl 阅读全文
posted @ 2013-05-07 13:03 tangr206 阅读(552) 评论(0) 推荐(0)
摘要:Sincestr.format()is quite new, a lot of Python code still uses the%operator. However, because this old style of formatting will eventually be removed from the language,str.format()should generally be used.More information can be found in theString Formatting Operationssection.老式的("%s id %s" 阅读全文
posted @ 2013-05-06 13:10 tangr206 阅读(265) 评论(0) 推荐(0)
摘要:AstralWind的博客上有对函数式编程很好地讲解,一下做一些节选1.1. 什么是函数式编程?函数式编程使用一系列的函数解决问题。函数仅接受输入并产生输出,不包含任何能影响产生输出的内部状态。任何情况下,使用相同的参数调用函数始终能产生同样的结果。 在一个函数式的程序中,输入的数据“流过”一系列的函数,每一个函数根据它的输入产生输出。函数式风格避免编写有“边界效应”(side effects)的函数:修改内部状态,或者是其他无法反应在输出上的变化。完全没有边界效应的函数被称为“纯函数式的”(purely functional)。避免边界效应意味着不使用在程序运行时可变的数据结构,输出只依赖. 阅读全文
posted @ 2013-05-03 16:11 tangr206 阅读(238) 评论(0) 推荐(0)
摘要:dict设置默认值 “” A common use of dictionaries is to count occurrences by setting the value of d[key] to 1 on its first occurrence, then increment the value on each subsequent occurrence. This can be done several different ways, but the get() method is the most succinct: ”“” ... 阅读全文
posted @ 2013-04-16 18:29 tangr206 阅读(302) 评论(0) 推荐(0)
摘要:参考:http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/http://blog.shine-it.net/python/dive-into-generator您可能听说过,带有 yield 的函数在 Python 中被称之为 generator(生成器),何谓 generator ?我们先抛开 generator,以一个常见的编程题目来展示 yield 的概念。清单 1. 简单输出斐波那契數列前 N 个数 def fab(max): n, a, b = 0, 0, 1 while n <... 阅读全文
posted @ 2013-04-09 20:05 tangr206 阅读(1625) 评论(0) 推荐(0)
摘要:@ http://kenby.iteye.com/blog/1159621一. Tornado是什么?Facebook发布了开源网络服务器框架Tornado,该平台基于Facebook刚刚收购的社交聚合网站FriendFeed的实时信息服务开发而来.Tornado由Python编写,是一款轻量级的Web服务器,同时又是一个开发框架。采用非阻塞I/O模型(epoll),主要是为了应对高并发 访问量而被开发出来,尤其适用于comet应用。二. 为什么要阅读Tornado的源代码Tornado由前google员工开发, 代码非常精练, 实现也很轻巧, 加上清晰的注释和丰富的demo, 我们可以很容易 阅读全文
posted @ 2013-03-21 21:33 tangr206 阅读(435) 评论(0) 推荐(0)
摘要:json 格式的例子: { "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumbers& 阅读全文
posted @ 2011-11-21 17:04 tangr206 阅读(697) 评论(0) 推荐(0)