Python论坛  - 讨论区

标题:[python-chinese] Python的局部变量作用域不同于C++的地方

2006年10月10日 星期二 17:32

Linker Lin linkerlin88在gmail.com
星期二 十月 10 17:32:15 HKT 2006

def a():
    k=1

def main():
    if 0:
        j=1
    else:
        k=2

    try:
        i=1
        #throw i
        print x
    except:
        print i,j,k#i,j,k都可以访问



main()


输出:
1 1 2

-- 
Linker M Lin
linkerlin88在gmail.com
linker.m.lin在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/1cbaa676/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 17:49

Yunfeng Tao taoyfeng在gmail.com
星期二 十月 10 17:49:49 HKT 2006

in python 2.5, the result is:

1
Traceback (most recent call last):
  File "test.py", line 18, in 
    main()
  File "test.py", line 14, in main
    print i,j,k
UnboundLocalError: local variable 'j' referenced before assignment

2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> def a():
>     k=1
>
> def main():
>     if 0:
>         j=1
>     else:
>         k=2
>
>     try:
>         i=1
>         #throw i
>         print x
>     except:
>         print i,j,k#i,j,k都可以访问
>
>
>
> main()
>
>
> 输出:
> 1 1 2
>
> --
> Linker M Lin
> linkerlin88在gmail.com
> linker.m.lin在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 18:20

Linker Lin linkerlin88在gmail.com
星期二 十月 10 18:20:21 HKT 2006

我在Python2.4.3里面试的。

On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
>
> in python 2.5, the result is:
>
> 1
> Traceback (most recent call last):
>   File "test.py", line 18, in 
>     main()
>   File "test.py", line 14, in main
>     print i,j,k
> UnboundLocalError: local variable 'j' referenced before assignment
>
> 2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> > def a():
> >     k=1
> >
> > def main():
> >     if 0:
> >         j=1
> >     else:
> >         k=2
> >
> >     try:
> >         i=1
> >         #throw i
> >         print x
> >     except:
> >         print i,j,k#i,j,k都可以访问
> >
> >
> >
> > main()
> >
> >
> > 输出:
> > 1 1 2
> >
> > --
> > Linker M Lin
> > linkerlin88在gmail.com
> > linker.m.lin在gmail.com
> >   ※※※※※※※※※
> >   ※※我思故我在※※
> >   ※※※※※※※※※
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Linker M Lin
linkerlin88在gmail.com
linker.m.lin在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/18c0edde/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 18:31

Linker Lin linkerlin88在gmail.com
星期二 十月 10 18:31:18 HKT 2006

在Py25里面:
def a():
    k=1

def main():
    if 0:
        j=1
    else:
        k=2

    try:
        i=1#注意:i应该不能在except中访问才对
        #throw i
        print x
    except:
        print i#,j,k



main()


输出:
1
还是和C++的局部变量的定义不同。

On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
>
> in python 2.5, the result is:
>
> 1
> Traceback (most recent call last):
>   File "test.py", line 18, in 
>     main()
>   File "test.py", line 14, in main
>     print i,j,k
> UnboundLocalError: local variable 'j' referenced before assignment
>
> 2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> > def a():
> >     k=1
> >
> > def main():
> >     if 0:
> >         j=1
> >     else:
> >         k=2
> >
> >     try:
> >         i=1
> >         #throw i
> >         print x
> >     except:
> >         print i,j,k#i,j,k都可以访问
> >
> >
> >
> > main()
> >
> >
> > 输出:
> > 1 1 2
> >
> > --
> > Linker M Lin
> > linkerlin88在gmail.com
> > linker.m.lin在gmail.com
> >   ※※※※※※※※※
> >   ※※我思故我在※※
> >   ※※※※※※※※※
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Linker M Lin
linkerlin88在gmail.com
linker.m.lin在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/56128536/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 18:33

Linker Lin linkerlin88在gmail.com
星期二 十月 10 18:33:39 HKT 2006

但是:
    if 0:
        j=1
    else:
        k=2
        k=j #UnboundLocalError: local variable 'j' referenced before
assignment

On 10/10/06, Linker Lin <linkerlin88在gmail.com> wrote:
>
> 在Py25里面:
> def a():
>     k=1
>
> def main():
>     if 0:
>         j=1
>     else:
>         k=2
>
>     try:
>         i=1#注意:i应该不能在except中访问才对
>         #throw i
>         print x
>     except:
>         print i#,j,k
>
>
>
> main()
>
>
> 输出:
> 1
> 还是和C++的局部变量的定义不同。
>
> On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
> >
> > in python 2.5, the result is:
> >
> > 1
> > Traceback (most recent call last):
> >   File "test.py", line 18, in 
> >     main()
> >   File "test.py", line 14, in main
> >     print i,j,k
> > UnboundLocalError: local variable 'j' referenced before assignment
> >
> > 2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> > > def a():
> > >     k=1
> > >
> > > def main():
> > >     if 0:
> > >         j=1
> > >     else:
> > >         k=2
> > >
> > >     try:
> > >         i=1
> > >         #throw i
> > >         print x
> > >     except:
> > >         print i,j,k#i,j,k都可以访问
> > >
> > >
> > >
> > > main()
> > >
> > >
> > > 输出:
> > > 1 1 2
> > >
> > > --
> > > Linker M Lin
> > > linkerlin88在gmail.com
> > > linker.m.lin在gmail.com
> > >   ※※※※※※※※※
> > >   ※※我思故我在※※
> > >   ※※※※※※※※※
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> Linker M Lin
> linkerlin88在gmail.com
> linker.m.lin在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
>



-- 
Linker M Lin
linkerlin88在gmail.com
linker.m.lin在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/bc69e753/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 18:38

Linker Lin linkerlin88在gmail.com
星期二 十月 10 18:38:30 HKT 2006

看来Python25的局部变量作用域还是定义的有点问题。


On 10/10/06, Linker Lin <linkerlin88在gmail.com> wrote:
>
> 但是:
>     if 0:
>         j=1
>     else:
>         k=2
>         k=j #UnboundLocalError: local variable 'j' referenced before
> assignment
>
> On 10/10/06, Linker Lin <linkerlin88在gmail.com> wrote:
> >
> > 在Py25里面:
> > def a():
> >     k=1
> >
> > def main():
> >     if 0:
> >         j=1
> >     else:
> >         k=2
> >
> >     try:
> >         i=1#注意:i应该不能在except中访问才对
> >         #throw i
> >         print x
> >     except:
> >         print i#,j,k
> >
> >
> >
> > main()
> >
> >
> > 输出:
> > 1
> > 还是和C++的局部变量的定义不同。
> >
> > On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
> > >
> > > in python 2.5, the result is:
> > >
> > > 1
> > > Traceback (most recent call last):
> > >   File "test.py", line 18, in 
> > >     main()
> > >   File "test.py", line 14, in main
> > >     print i,j,k
> > > UnboundLocalError: local variable 'j' referenced before assignment
> > >
> > > 2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> > > > def a():
> > > >     k=1
> > > >
> > > > def main():
> > > >     if 0:
> > > >         j=1
> > > >     else:
> > > >         k=2
> > > >
> > > >     try:
> > > >         i=1
> > > >         #throw i
> > > >         print x
> > > >     except:
> > > >         print i,j,k#i,j,k都可以访问
> > > >
> > > >
> > > >
> > > > main()
> > > >
> > > >
> > > > 输出:
> > > > 1 1 2
> > > >
> > > > --
> > > > Linker M Lin
> > > > linkerlin88在gmail.com
> > > > linker.m.lin在gmail.com
> > > >   ※※※※※※※※※
> > > >   ※※我思故我在※※
> > > >   ※※※※※※※※※
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Detail Info:
> > > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> >
> > --
> > Linker M Lin
> > linkerlin88在gmail.com
> > linker.m.lin在gmail.com
> >   ※※※※※※※※※
> >   ※※我思故我在※※
> >   ※※※※※※※※※
> >
>
>
>
> --
> Linker M Lin
> linkerlin88在gmail.com
> linker.m.lin在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
>



-- 
Linker M Lin
linkerlin88在gmail.com
linker.m.lin在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/30a4666b/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 18:49

迎风飘逸 pesoft在126.com
星期二 十月 10 18:49:59 HKT 2006

  这样感觉挺方便的啊!这种也应该是正确的啊!
    C++ 中的类似代码也是可以运行啊!
#include 
int main()
{
 try{
  int i=0xff;
  throw i;
 }
 catch(int a)
 {
  std::cout<< a<< std::endl;
 }
}
输出:255

迎风飘逸,pesoft at 126.com
2006-10-10 
----- Original Message ----- 
From: Linker Lin 
To: python-chinese 
Sent: 2006-10-10, 18:31:18
Subject: Re: [python-chinese]Python的局部变量作用域不同于C++的地方


在Py25里面:
def a():
    k=1

def main():    
    if 0:
        j=1
    else:
        k=2
    
    try:
        i=1#注意:i应该不能在except中访问才对 
        #throw i
        print x
    except:
        print i#,j,k



main()        


输出:
1
还是和C++的局部变量的定义不同。


On 10/10/06, Yunfeng Tao <taoyfeng at gmail.com> wrote:
in python 2.5, the result is:

1
Traceback (most recent call last):
  File "test.py", line 18, in 
    main()
  File "test.py", line 14, in main
    print i,j,k
UnboundLocalError: local variable 'j' referenced before assignment 

2006/10/10, Linker Lin <linkerlin88 at gmail.com>:
> def a():
>     k=1
>
> def main():
>     if 0:
>         j=1
>     else: 
>         k=2
>
>     try:
>         i=1
>         #throw i
>         print x
>     except:
>         print i,j,k#i,j,k都可以访问
>
>
>
> main()
>
>
> 输出:
> 1 1 2
>
> --
> Linker M Lin
> linkerlin88 at gmail.com
> linker.m.lin at gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn 
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
_______________________________________________ 
python-chinese
Post: send python-chinese at lists.python.cn
Subscribe: send subscribe to python-chinese-request at lists.python.cn 
Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese 



-- 
Linker M Lin
linkerlin88 at gmail.com
linker.m.lin at gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※ 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/b22ad92e/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 19:25

Linker Lin linkerlin88在gmail.com
星期二 十月 10 19:25:07 HKT 2006

C++:
i不能在catch(){中访问}


On 10/10/06, 迎风飘逸 <pesoft在126.com> wrote:
>
>  这样感觉挺方便的啊!这种也应该是正确的啊!
>     C++ 中的类似代码也是可以运行啊!
> #include 
> int main()
> {
>  try{
>   int i=0xff;
>   throw i;
>  }
>  catch(int a)
>  {
>   std::cout<< a<< std::endl;
>  }
> }
> 输出:255
>
> 迎风飘逸,pesoft在126.com<%3C%21--AID_FROMADDRESS_BEGIN--%3Epesoft在126.com%3C%21--AID_FROMADDRESS_END--%3E>
> 2006-10-10
>
> ----- Original Message -----
> *From: *Linker Lin <linkerlin88在gmail.com>
> *To: *python-chinese <python-chinese在lists.python.cn>
> *Sent: *2006-10-10, 18:31:18
> *Subject: *Re: [python-chinese]Python的局部变量作用域不同于C++的地方
>
>  在Py25里面:
> def a():
>     k=1
>
> def main():
>     if 0:
>         j=1
>     else:
>         k=2
>
>     try:
>         i=1#注意:i应该不能在except中访问才对
>         #throw i
>         print x
>     except:
>         print i#,j,k
>
>
>
> main()
>
>
> 输出:
> 1
> 还是和C++的局部变量的定义不同。
>
> On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
> >
> > in python 2.5, the result is:
> >
> > 1
> > Traceback (most recent call last):
> >   File "test.py", line 18, in 
> >     main()
> >   File "test.py", line 14, in main
> >     print i,j,k
> > UnboundLocalError: local variable 'j' referenced before assignment
> >
> > 2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> > > def a():
> > >     k=1
> > >
> > > def main():
> > >     if 0:
> > >         j=1
> > >     else:
> > >         k=2
> > >
> > >     try:
> > >         i=1
> > >         #throw i
> > >         print x
> > >     except:
> > >         print i,j,k#i,j,k都可以访问
> > >
> > >
> > >
> > > main()
> > >
> > >
> > > 输出:
> > > 1 1 2
> > >
> > > --
> > > Linker M Lin
> > > linkerlin88在gmail.com
> > > linker.m.lin在gmail.com
> > >   ※※※※※※※※※
> > >   ※※我思故我在※※
> > >   ※※※※※※※※※
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> Linker M Lin
> linkerlin88在gmail.com
> linker.m.lin在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Linker M Lin
linkerlin88在gmail.com
linker.m.lin在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/8bf66a91/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 19:46

Linker Lin linkerlin88在gmail.com
星期二 十月 10 19:46:15 HKT 2006

非常奇怪Python25里面,为什么try:里面定义的变量在except:里面是可见的,
但是if:里面定义的变量,在else:里面不可见。
谁能指点一下?

On 10/10/06, Linker Lin <linkerlin88在gmail.com> wrote:
>
> C++:
> i不能在catch(){中访问}
>
>
> On 10/10/06, 迎风飘逸 <pesoft在126.com> wrote:
>
> >  这样感觉挺方便的啊!这种也应该是正确的啊!
> >     C++ 中的类似代码也是可以运行啊!
> > #include 
> > int main()
> > {
> >  try{
> >   int i=0xff;
> >   throw i;
> >  }
> >  catch(int a)
> >  {
> >   std::cout<< a<< std::endl;
> >  }
> > }
> > 输出:255
> >
> > 迎风飘逸,pesoft在126.com<%3C%21--AID_FROMADDRESS_BEGIN--%3Epesoft在126.com%3C%21--AID_FROMADDRESS_END--%3E>
> > 2006-10-10
> >
> > ----- Original Message -----
> >  *From: *Linker Lin <linkerlin88在gmail.com>
> > *To: *python-chinese <python-chinese在lists.python.cn>
> > *Sent: *2006-10-10, 18:31:18
> > *Subject: *Re: [python-chinese]Python的局部变量作用域不同于C++的地方
> >
> >  在Py25里面:
> > def a():
> >     k=1
> >
> > def main():
> >     if 0:
> >         j=1
> >     else:
> >         k=2
> >
> >     try:
> >         i=1#注意:i应该不能在except中访问才对
> >         #throw i
> >         print x
> >     except:
> >         print i#,j,k
> >
> >
> >
> > main()
> >
> >
> > 输出:
> > 1
> > 还是和C++的局部变量的定义不同。
> >
> > On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
> > >
> > > in python 2.5, the result is:
> > >
> > > 1
> > > Traceback (most recent call last):
> > >   File "test.py", line 18, in 
> > >     main()
> > >   File "test.py", line 14, in main
> > >     print i,j,k
> > > UnboundLocalError: local variable 'j' referenced before assignment
> > >
> > > 2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> > > > def a():
> > > >     k=1
> > > >
> > > > def main():
> > > >     if 0:
> > > >         j=1
> > > >     else:
> > > >         k=2
> > > >
> > > >     try:
> > > >         i=1
> > > >         #throw i
> > > >         print x
> > > >     except:
> > > >         print i,j,k#i,j,k都可以访问
> > > >
> > > >
> > > >
> > > > main()
> > > >
> > > >
> > > > 输出:
> > > > 1 1 2
> > > >
> > > > --
> > > > Linker M Lin
> > > > linkerlin88在gmail.com
> > > > linker.m.lin在gmail.com
> > > >   ※※※※※※※※※
> > > >   ※※我思故我在※※
> > > >   ※※※※※※※※※
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Detail Info:
> > > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> >
> > --
> > Linker M Lin
> > linkerlin88在gmail.com
> > linker.m.lin在gmail.com
> >   ※※※※※※※※※
> >   ※※我思故我在※※
> >   ※※※※※※※※※
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> Linker M Lin
> linkerlin88在gmail.com
> linker.m.lin在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
>



-- 
Linker M Lin
linkerlin88在gmail.com
linker.m.lin在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061010/5055072c/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 20:47

Yunfeng Tao taoyfeng在gmail.com
星期二 十月 10 20:47:31 HKT 2006

考虑一下如下代码:

try:
    s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s1.bind(("", 9000))
    s2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s2.bind(("", 9001))
except:
    raise Exception("ports are not available.")
else:
    s1.close()
    s2.close()

在这里,else:里能访问try:里定义的变量非常方便;类似地,except:里能访问try:里定义的变量也非常方便于资源的回收。

2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> 非常奇怪Python25里面,为什么try:里面定义的变量在except:里面是可见的,
> 但是if:里面定义的变量,在else:里面不可见。
> 谁能指点一下?
>
>
> On 10/10/06, Linker Lin < linkerlin88在gmail.com> wrote:
> > C++:
> > i不能在catch(){中访问}
> >
> >
> >
> >
> > On 10/10/06, 迎风飘逸 <pesoft在126.com> wrote:
> > >
> > >
> > >
> > >   这样感觉挺方便的啊!这种也应该是正确的啊!
> > >     C++ 中的类似代码也是可以运行啊!
> > > #include 
> > > int main()
> > > {
> > >  try{
> > >   int i=0xff;
> > >   throw i;
> > >  }
> > >  catch(int a)
> > >  {
> > >   std::cout<< a<< std::endl;
> > >  }
> > > }
> > > 输出:255
> > >
> > > 迎风飘逸,pesoft在126.com
> > > 2006-10-10
> > >
> > >
> > > ----- Original Message -----
> > > From: Linker Lin
> > > To: python-chinese
> > > Sent: 2006-10-10, 18:31:18
> > > Subject: Re: [python-chinese]Python的局部变量作用域不同于C++的地方
> > >
> > >
> > >
> > >
> > > 在Py25里面:
> > > def a():
> > >     k=1
> > >
> > > def main():
> > >     if 0:
> > >         j=1
> > >     else:
> > >         k=2
> > >
> > >     try:
> > >         i=1#注意:i应该不能在except中访问才对
> > >         #throw i
> > >         print x
> > >     except:
> > >         print i#,j,k
> > >
> > >
> > >
> > > main()
> > >
> > >
> > > 输出:
> > > 1
> > > 还是和C++的局部变量的定义不同。
> > >
> > >
> > > On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
> > > > in python 2.5, the result is:
> > > >
> > > > 1
> > > > Traceback (most recent call last):
> > > >   File "test.py", line 18, in 
> > > >     main()
> > > >   File "test.py", line 14, in main
> > > >     print i,j,k
> > > > UnboundLocalError: local variable 'j' referenced before assignment
> > > >
> > > > 2006/10/10, Linker Lin <linkerlin88在gmail.com>:
> > > > > def a():
> > > > >     k=1
> > > > >
> > > > > def main():
> > > > >     if 0:
> > > > >         j=1
> > > > >     else:
> > > > >         k=2
> > > > >
> > > > >     try:
> > > > >         i=1
> > > > >         #throw i
> > > > >         print x
> > > > >     except:
> > > > >         print i,j,k#i,j,k都可以访问
> > > > >
> > > > >
> > > > >
> > > > > main()
> > > > >
> > > > >
> > > > > 输出:
> > > > > 1 1 2
> > > > >
> > > > > --
> > > > > Linker M Lin
> > > > > linkerlin88在gmail.com
> > > > > linker.m.lin在gmail.com
> > > > >   ※※※※※※※※※
> > > > >   ※※我思故我在※※
> > > > >   ※※※※※※※※※
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese在lists.python.cn
> > > > > Subscribe: send subscribe to
> > > > > python-chinese-request在lists.python.cn
> > > > > Unsubscribe: send unsubscribe to
> > > > > python-chinese-request在lists.python.cn
> > > > > Detail Info:
> > > > > http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > > > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > >
> > > --
> > > Linker M Lin
> > > linkerlin88在gmail.com
> > > linker.m.lin在gmail.com
> > >   ※※※※※※※※※
> > >   ※※我思故我在※※
> > >   ※※※※※※※※※
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> >
> >
> >
> > --
> > Linker M Lin
> > linkerlin88在gmail.com
> > linker.m.lin在gmail.com
> >   ※※※※※※※※※
> >   ※※我思故我在※※
> >   ※※※※※※※※※
>
>
>
> --
>
> Linker M Lin
> linkerlin88在gmail.com
> linker.m.lin在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月10日 星期二 20:52

limodou limodou在gmail.com
星期二 十月 10 20:52:01 HKT 2006

On 10/10/06, Yunfeng Tao <taoyfeng在gmail.com> wrote:
> 考虑一下如下代码:
>
> try:
>     s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>     s1.bind(("", 9000))
>     s2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>     s2.bind(("", 9001))
> except:
>     raise Exception("ports are not available.")
> else:
>     s1.close()
>     s2.close()
>
> 在这里,else:里能访问try:里定义的变量非常方便;类似地,except:里能访问try:里定义的变量也非常方便于资源的回收。
>
方便是一回事,我前面已经说过了在python的函数中,一个变量是函数可见的。没有块方式的作用域。

-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号