From 3e049ade4a89e1f85ad8c094936bcbf7f78b973a Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 23 Mar 2016 13:04:00 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BD=91=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 415e048..4a0ff09 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ 本人边学习Python3,边把一些小脚本(自己写的和网上找的)经过本机测试后然后同步到github上,供初学者下载研究,欢迎fork,也欢迎提交新的Python小脚本,特别是一些有趣好玩的小程序!有什么问题可以通过Issues讨论,喜欢的话点Star或分享给别人哦! 学习Python3记录博客: -http://www.tantengvip.com/category/develop/python3/ +http://blog.tanteng.me/category/develop/python3/ 注:IDE是PyCharm4.5,Python版本是3.4.3 From 13c3f88b7732eb173721b9be55734dcac5263c66 Mon Sep 17 00:00:00 2001 From: "lake.lai" Date: Tue, 19 Apr 2016 20:26:03 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=B8=80=E8=88=AC=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=EF=BC=8C=E4=B8=80=E4=B8=AA=E5=87=BD=E6=95=B0=E7=BB=93?= =?UTF-8?q?=E6=9D=9F=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AAreturn=E5=9C=A8=E4=BB=A3=E7=A0=81=E4=B8=8A=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E7=BE=8E=E8=A7=82=E3=80=82=E5=8F=AF=E4=BB=A5=E6=9B=B4?= =?UTF-8?q?=E6=B8=85=E6=A5=9A=E7=9A=84=E7=9F=A5=E9=81=93=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=9C=A8=E5=93=AA=E9=87=8C=E7=BB=93=E6=9D=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 虽然不加也可以,但是建议加上。这样可以更清楚的知道代码在哪里结束。如果您用过try...except...else...finily结构的话,会明白的 --- args_kwargs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/args_kwargs.py b/args_kwargs.py index 67edeb8..4095ca7 100644 --- a/args_kwargs.py +++ b/args_kwargs.py @@ -7,6 +7,7 @@ def alias(*args, **kwargs): print('args=', args) print('kwargs=', kwargs) + return alias(3, 23, 3, 3,a='hello',b=3,c='C') From 8255a5e82f642c10da0bf5a746d2ca89bf13b563 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 22 Apr 2016 09:47:22 +0800 Subject: [PATCH 3/4] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4a0ff09..745e4be 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ http://blog.tanteng.me/category/develop/python3/ 注:IDE是PyCharm4.5,Python版本是3.4.3 + +欢迎大家提交好的学习Python3的脚本,我会合并进来! From 6347e16a49e565705145b2f059db1340a6f21b7a Mon Sep 17 00:00:00 2001 From: "lake.lai" Date: Mon, 25 Apr 2016 15:16:05 +0800 Subject: [PATCH 4/4] =?UTF-8?q?python=E4=B8=AD=E7=9A=84=E9=97=AD=E5=8C=85?= =?UTF-8?q?=20for=20python=203.x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UnboundLocalError: local variable 'count' referenced before assignment. 什么意思?就是说conut这个变量你没有定义就直接引用了,我不知道这是个什么东西,程序就崩溃了.于是,再python3里面,引入了一个关键字:nonlocal,这个关键字是干什么的?就是告诉python程序,我的这个count变量是再外部定义的,你去外面找吧.然后python就去外层函数找,然后就找到了count=0这个定义和赋值,程序就能正常执行了. --- closure.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/closure.py b/closure.py index 47dd67c..57fe5dd 100644 --- a/closure.py +++ b/closure.py @@ -1,8 +1,9 @@ def hellocounter (name): - count=[0] + count=0 #PYTHON 2.x 中,要写count=[0] def counter(): - count[0]+=1 - print('Hello,',name,',',count[0],' access!') + nonlocal count #PYTHON 2.x 中,此行和下一行要换成count[0]+=1 + count+=1 + print('Hello,',name,',',count,' access!')#PYTHON 2.x 中请自觉换成str(count[0]) return counter hello = hellocounter('ma6174') @@ -10,6 +11,18 @@ def counter(): hello() hello() +''' +执行结果 +>>> hello() +Hello, ma6174 , 1 access! +>>> hello() +Hello, ma6174 , 2 access! +>>> hello() +Hello, ma6174 , 3 access! +''' +##为什么PYTHON 2.x中不直接写count而用list?这是python2的一个bug,如果用count话,会报这样一个错误: +##UnboundLocalError: local variable 'count' referenced before assignment. + def make_adder(addend): def adder(augend): return augend + addend