关于python的两个入门问题
我使用的是python2.5 + eclipse + pydev
1。新建一个module时,为什么会产生一个__init__.py?这个文件有什么用?
2。看下面一段代码,为什么出错?
直接在一个py文件中输入:
index = 0
def test():
print index
if index >0 :
index = 1
else:
index = -1
test()
错误为:
Traceback (most recent call last):
File "D:\java\WORK32\TestPython\src\sdfd\SDf.py", line 10, in <module>
test()
File "D:\java\WORK32\TestPython\src\sdfd\SDf.py", line 4, in test
print index
UnboundLocalError: local variable 'index' referenced before assignment
我明白它的意思,但是我不知道怎么实现我的要求:
在test()中的index使用之前声明的,而不是在if else中重新声明
多谢大家!