易截截图软件、单文件、免安装、纯绿色、仅160KB

Python的bug

Python太火了,不学点都感觉自己不是学计算机的,今天看了个不错的《简明python教程》,很不错。不过在学习过程中,居然发现了一个Python的bug,
#!/usr/bin/python
#coding=UTF-8
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person's data.'''
self.name = name
print '(Initializing %s)' % self.name
Person.population += 1
def __del__(self):
'''I am dying'''
print '%s syas bye.' % self.name
Person.population -= 1
if Person.population == 0:
print 'I am the last one.'
else:
print 'There are still %d people left.' % \
Person.population
def sayHi(self):
'''Greeting by the person.
Really, that's all it does'''
print 'Hi, my name is %s.' % self.name
def howMany(self):
'''Prints the current population'''
if Person.population == 1:
print "I am the only person here"
else:
print 'We have %d persons here.' % Person.population
luolei = Person('luolei')
luolei.sayHi()
luolei.howMany()
xiaoming = Person('Xiao Ming')
xiaoming.sayHi()
xiaoming.howMany()
luolei.sayHi()
luolei.howMany() 
Person在对象析构之前先析构了,在对象析构的时候找不到类的说明了。
避免这个bug的办法是在luolei前面加上一个_,即变成_luolei则没有问题,因为python会先析构前面有下划线的变量,如此NB的Python居然有这么个明显的问题,困惑
Python的版本:Python 2.6.4


相关文档:

Python中RE模块的应用

      Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。Python 1.5之前版本则是通过 regex 模块提供 Emecs 风格的模式。Emacs 风格模式可读性稍差些,而且功能也不强,因此编写新代码时尽量不要再使用 regex 模块,当然偶尔你还是可能在老代码里发现其踪影。
  & ......

python中eval, exec, execfile,和compile


eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果。
同样地, exec语句将字符串str当成有效Python代码来执行.提供给exec的代码的名称空间和exec语句的名称空间相同.
最后,execfile(filename [,globals [,locals ]])函数可以用来执行一个文件,看下面的例子:
>>> ev ......

Python矩阵转置中的二维数组的实际操作方案介绍

如果你对Python矩阵转置的实际应用操作方案的转置不知道如何进行下一步时,你就你需要转置一个二维数组,将Python矩阵转置的行列互换.
这样就可以完成你所需要的应用操作,以下是文章的具体操作。
你需要转置一个二维数组,将行列互换,讨论:你需要确保该数组的行列数都是相同的.比如:
arr
 = [[1, 2,&nbs ......

vc中调用python代码

运行一句python命令
对vc设置路径
include:D:\PYTHON31\INCLUDE
lib:D:\PYTHON31\LIBS
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
 Py_Initialize() ;
 PyRun_SimpleString("print('Hello')");
 //PyRun_SimpleString("print(dir())");
 Py_Finalize();& ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号