Python论坛  - 讨论区

标题:[python-chinese] 函数嵌套

2006年11月10日 星期五 14:36

leopay leopay在gmail.com
星期五 十一月 10 14:36:09 HKT 2006

def test1():
a=1
def test2():
print a
a+=1
test2()
test1()

可以print a为1,说明能访问到test1的a,但是改变a值时就报错了?

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

2006年11月10日 星期五 14:48

leopay leopay在gmail.com
星期五 十一月 10 14:48:54 HKT 2006

¸ñʽÂÒÁËÖØ·¢Ò»ÏÂ

def test1():
    a=1
    def test2():
        print a
        a+=1
    test2()
test1()

¿ÉÒÔprint aΪ1£¬ËµÃ÷ÄÜ·ÃÎʵ½test1µÄa£¬µ«ÊǸıäaֵʱ¾Í±¨´íÁË£¿
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061110/fecef980/attachment.htm 

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

2006年11月10日 星期五 15:05

limodou limodou在gmail.com
星期五 十一月 10 15:05:55 HKT 2006

On 11/10/06, leopay <leopay在gmail.com> wrote:
> 格式乱了重发一下
>
>
> def test1():
>     a=1
>     def test2():
>         print a
>         a+=1
>     test2()
> test1()
>
> 可以print a为1,说明能访问到test1的a,但是改变a值时就报错了?

对于上例,a += 1 可以看成 a = a +
1。python在处理时会先编译,因为有赋值操作,因此python在编译时会设置a是一个局部变量。但在执行时,这个赋值操作又要用到a,但是在test2函数中,在a
+= 1之前没有对a的赋值操作,因此python会认为a还没有定义。

我想正是因为先编译后执行的方式造成了这种结果。所以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]

2006年11月10日 星期五 15:50

=?GB2312?B?u8aPqg==?= yellowfool在gmail.com
星期五 十一月 10 15:50:20 HKT 2006

leopay:
ÄãÔÚʲô»·¾³ÏÂÔËÐеģ¿ÎÒÕâÀï2.4.3ºÍ2.3.5ÏÔʾһÑù¾ùΪ
Traceback (most recent call last):
  File "D:\pythonwork\firsttest\src\test\test4.py", line 7, in ?
    test1()
  File "D:\pythonwork\firsttest\src\test\test4.py", line 6, in test1
    test2()
  File "D:\pythonwork\firsttest\src\test\test4.py", line 4, in test2
    print a
UnboundLocalError: local variable 'a' referenced before assignment


ÔÚ06-11-10£¬leopay <leopay在gmail.com> дµÀ£º
>
> ¸ñʽÂÒÁËÖØ·¢Ò»ÏÂ
>
> def test1():
>     a=1
>     def test2():
>         print a
>         a+=1
>     test2()
> test1()
>
> ¿ÉÒÔprint aΪ1£¬ËµÃ÷ÄÜ·ÃÎʵ½test1µÄa£¬µ«ÊǸıäaֵʱ¾Í±¨´íÁË£¿
>
> _______________________________________________
> 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
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061110/549a4912/attachment.htm 

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

2006年11月10日 星期五 16:37

天才狐狸 mem_fox在263.net
星期五 十一月 10 16:37:31 HKT 2006

ÊÇ°¡£¡ÎÒµÄÒ²ÊDZ¨¸æ˵ ¡°UnboundLocalError: local variable 'a' referenced before assignment¡±

----- Original Message ----- 
From: »Æª 
To: python-chinese at lists.python.cn 
Sent: Friday, November 10, 2006 3:50 PM
Subject: Re: [python-chinese] º¯ÊýǶÌ×


leopay:
ÄãÔÚʲô»·¾³ÏÂÔËÐеģ¿ÎÒÕâÀï2.4.3ºÍ2.3.5ÏÔʾһÑù¾ùΪ
Traceback (most recent call last):
  File "D:\pythonwork\firsttest\src\test\test4.py", line 7, in ?
    test1()
  File "D:\pythonwork\firsttest\src\test\test4.py", line 6, in test1
    test2() 
  File "D:\pythonwork\firsttest\src\test\test4.py", line 4, in test2
    print a
UnboundLocalError: local variable 'a' referenced before assignment

 
ÔÚ06-11-10£¬leopay <leopay at gmail.com> дµÀ£º 
¸ñʽÂÒÁËÖØ·¢Ò»Ï 


def test1():
    a=1
    def test2():
        print a
        a+=1
    test2()
test1()

¿ÉÒÔprint aΪ1£¬ËµÃ÷ÄÜ·ÃÎʵ½test1µÄa£¬µ«ÊǸıäaֵʱ¾Í±¨´íÁË£¿ 

_______________________________________________
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

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

2006年11月10日 星期五 17:10

fdu.xiaojf在gmail.com fdu.xiaojf在gmail.com
星期五 十一月 10 17:10:49 HKT 2006

Ìì²ÅºüÀê wrote:
> ÊÇ°¡£¡ÎÒµÄÒ²ÊDZ¨¸æ˵ ¡°UnboundLocalError: local variable 'a' referenced before assignment¡±
>
> ----- Original Message ----- 
> From: »Æª 
> To: python-chinese在lists.python.cn 
> Sent: Friday, November 10, 2006 3:50 PM
> Subject: Re: [python-chinese] º¯ÊýǶÌ×
>
>
> leopay:
> ÄãÔÚʲô»·¾³ÏÂÔËÐеģ¿ÎÒÕâÀï2.4.3ºÍ2.3.5ÏÔʾһÑù¾ùΪ
> Traceback (most recent call last):
>   File "D:\pythonwork\firsttest\src\test\test4.py", line 7, in ?
>     test1()
>   File "D:\pythonwork\firsttest\src\test\test4.py", line 6, in test1
>     test2() 
>   File "D:\pythonwork\firsttest\src\test\test4.py", line 4, in test2
>     print a
> UnboundLocalError: local variable 'a' referenced before assignment
>
>  
> ÔÚ06-11-10£¬leopay <leopay在gmail.com> дµÀ£º 
> ¸ñʽÂÒÁËÖØ·¢Ò»Ï 
>
>
> def test1():
>     a=1
>     def test2():
>         print a
>         a+=1
>     test2()
> test1()
>
> ¿ÉÒÔprint aΪ1£¬ËµÃ÷ÄÜ·ÃÎʵ½test1µÄa£¬µ«ÊǸıäaֵʱ¾Í±¨´íÁË£¿ 
>
>   
ÕâÖÖº¯ÊýǶÌ׶¨ÒåÊDz»±»ÌᳫµÄ.

²»¹ýÎÒÒ²²»Çå³þÎÊʲô²»ÐÐ.

²Â²âÒ»ÏÂ:
ÔÚtest2ÀïÃæ¼ÙÈçûÓи³ÖµÓï¾ä,"print a"ËùÒýÓõÄaÊÇÉÏÒ»²ã½á¹¹(test1)ÀïÃæµÄa.
µ«ÊǼÓÒ»¸ö¸³ÖµÓï¾ä"a+=1",ĬÈϾÍÊÇÒª°Ñtest2ÀïÃæµÄÒ»¸ö¾Ö²¿±äÁ¿aµÄÖµ¼Ó1,µ«
ÊÇtest2ÄÚ²¿ÊÇûÓоֲ¿±äÁ¿aµÄ.


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

2006年11月10日 星期五 18:08

厉兵 imlibing在163.com
星期五 十一月 10 18:08:13 HKT 2006

ËûÒâ˼ÊÇ
def test1():
    a=1
    def test2():
        print a
    test2()
test1()
¿ÉÒÔ°É£¬ÎÒÏë
  ----- Original Message ----- 
  From: »Æª 
  To: python-chinese在lists.python.cn 
  Sent: Friday, November 10, 2006 3:50 PM
  Subject: Re: [python-chinese] º¯ÊýǶÌ×


  leopay:
  ÄãÔÚʲô»·¾³ÏÂÔËÐеģ¿ÎÒÕâÀï2.4.3ºÍ2.3.5ÏÔʾһÑù¾ùΪ
  Traceback (most recent call last):
    File "D:\pythonwork\firsttest\src\test\test4.py", line 7, in ?
      test1()
    File "D:\pythonwork\firsttest\src\test\test4.py", line 6, in test1
      test2() 
    File "D:\pythonwork\firsttest\src\test\test4.py", line 4, in test2
      print a
  UnboundLocalError: local variable 'a' referenced before assignment

   
  ÔÚ06-11-10£¬leopay <leopay在gmail.com> дµÀ£º 
    ¸ñʽÂÒÁËÖØ·¢Ò»Ï 


    def test1():
        a=1
        def test2():
            print a
            a+=1
        test2()
    test1()

    ¿ÉÒÔprint aΪ1£¬ËµÃ÷ÄÜ·ÃÎʵ½test1µÄa£¬µ«ÊǸıäaֵʱ¾Í±¨´íÁË£¿ 

    _______________________________________________
    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
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061110/dca5abef/attachment.html 

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

2006年11月10日 星期五 18:28

毛旭峰 deneral在gmail.com
星期五 十一月 10 18:28:19 HKT 2006

ÒªÉùÃ÷GlobalµÄ°É



ëÐñ·å
2006-11-10



·¢¼þÈË£º À÷±ø
·¢ËÍʱ¼ä£º 2006-11-10 18:04:07
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ Re: [python-chinese]º¯ÊýǶÌ×

ËûÒâ˼ÊÇ
def test1():
    a=1
    def test2():
        print a
    test2()
test1()
¿ÉÒÔ°É£¬ÎÒÏë
----- Original Message ----- 
From: »Æª 
To: python-chinese在lists.python.cn 
Sent: Friday, November 10, 2006 3:50 PM
Subject: Re: [python-chinese] º¯ÊýǶÌ×


leopay:
ÄãÔÚʲô»·¾³ÏÂÔËÐеģ¿ÎÒÕâÀï2.4.3ºÍ2.3.5ÏÔʾһÑù¾ùΪ
Traceback (most recent call last):
  File "D:\pythonwork\firsttest\src\test\test4.py", line 7, in ?
    test1()
  File "D:\pythonwork\firsttest\src\test\test4.py", line 6, in test1
    test2() 
  File "D:\pythonwork\firsttest\src\test\test4.py", line 4, in test2
    print a
UnboundLocalError: local variable 'a' referenced before assignment

 
ÔÚ06-11-10£¬leopay <leopay在gmail.com> дµÀ£º 
¸ñʽÂÒÁËÖØ·¢Ò»Ï 


def test1():
    a=1
    def test2():
        print a
        a+=1
    test2()
test1()

¿ÉÒÔprint aΪ1£¬ËµÃ÷ÄÜ·ÃÎʵ½test1µÄa£¬µ«ÊǸıäaֵʱ¾Í±¨´íÁË£¿ 

_______________________________________________
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
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061110/5cf2657e/attachment.htm 

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

2006年11月10日 星期五 20:20

fdu.xiaojf在gmail.com fdu.xiaojf在gmail.com
星期五 十一月 10 20:20:19 HKT 2006

ëÐñ·å wrote:
> ÒªÉùÃ÷GlobalµÄ°É
> ------------------------------------------------------------------------
> ëÐñ·å
> 2006-11-10
> ------------------------------------------------------------------------
> *·¢¼þÈË£º* À÷±ø
> *·¢ËÍʱ¼ä£º* 2006-11-10 18:04:07
> *ÊÕ¼þÈË£º* python-chinese在lists.python.cn
> *³­ËÍ£º*
> *Ö÷Ì⣺* Re: [python-chinese]º¯ÊýǶÌ×
> ËûÒâ˼ÊÇ
> def test1():
> a=1
> def test2():
> print a
> test2()
> test1()
> ¿ÉÒÔ°É£¬ÎÒÏë
>
>     ----- Original Message -----
>     *From:* »Æª yellowfool在gmail.com>
>     *To:* python-chinese在lists.python.cn
>     python-chinese在lists.python.cn>
>     *Sent:* Friday, November 10, 2006 3:50 PM
>     *Subject:* Re: [python-chinese] º¯ÊýǶÌ×
>
>     leopay:
>     ÄãÔÚʲô»·¾³ÏÂÔËÐеģ¿ÎÒÕâÀï2.4.3ºÍ2.3.5ÏÔʾһÑù¾ùΪ
>     Traceback (most recent call last):
>     File "D:\pythonwork\firsttest\src\test\test4.py", line 7, in ?
>     test1()
>     File "D:\pythonwork\firsttest\src\test\test4.py", line 6, in test1
>     test2()
>     File "D:\pythonwork\firsttest\src\test\test4.py", line 4, in test2
>     print a
>     UnboundLocalError: local variable 'a' referenced before assignment
>
>     ÔÚ06-11-10£¬*leopay* <leopay在gmail.com leopay在gmail.com>>
>     дµÀ£º
>
>         ¸ñʽÂÒÁËÖØ·¢Ò»ÏÂ
>
>
>         def test1():
>         a=1
>         def test2():
>         print a
>         a+=1
>         test2()
>         test1()
>
>         ¿ÉÒÔprint aΪ1£¬ËµÃ÷ÄÜ·ÃÎʵ½test1µÄa£¬µ«ÊǸıäaֵʱ¾Í±¨´íÁË£¿
>
>         _______________________________________________
>         python-chinese
>         Post: send python-chinese在lists.python.cn
>         python-chinese在lists.python.cn>
>         Subscribe: send subscribe to
>         python-chinese-request在lists.python.cn
>         python-chinese-request在lists.python.cn>
>         Unsubscribe: send unsubscribe to
>         python-chinese-request在lists.python.cn
>         python-chinese-request在lists.python.cn>
>         Detail Info: http://python.cn/mailman/listinfo/python-chinese
>         <http://python.cn/mailman/listinfo/python-chinese>
>
>
>     ------------------------------------------------------------------------
>
ÉùÃ÷globalÒ²²»ÐеİÉ,a²»ÊÇglobalµÄ,ÊÇtest1µÄ¾Ö²¿±äÁ¿


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

2006年11月10日 星期五 20:38

lu lubiao.py在gmail.com
星期五 十一月 10 20:38:19 HKT 2006

如果 python 不行

lua 恰好有这样的 特性

function f1()
    local a = 0
    function f2()
        a = a + 1
        return a
    end
    return f2
end

f = f1()
print(f())
print(f())
print(f())
print(f())
print(f())

a$ lua func.lua
1
2
3
4
5

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

2006年11月10日 星期五 20:44

洋陈 chenyang.cq在gmail.com
星期五 十一月 10 20:44:52 HKT 2006

pythonÖÐÓ¦¸ÃÕâÑùд

def f1():
     a = [0]
     def f2():
         a[0] += 1
         return a[0]
     return f2



2006/11/10, lu <lubiao.py在gmail.com>:
>
> Èç¹û python ²»ÐÐ
>
> lua Ç¡ºÃÓÐÕâÑùµÄ ÌØÐÔ
>
> function f1()
>    local a = 0
>    function f2()
>        a = a + 1
>        return a
>    end
>    return f2
> end
>
> f = f1()
> print(f())
> print(f())
> print(f())
> print(f())
> print(f())
>
> a$ lua func.lua
> 1
> 2
> 3
> 4
> 5
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061110/a6c34c3b/attachment.html 

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

2006年11月10日 星期五 21:00

lu self.lu在gmail.com
星期五 十一月 10 21:00:40 HKT 2006

这样 a 不再是 值类型了
就 ok 了

On 11/10/06, 洋陈 <chenyang.cq at gmail.com> wrote:
> python中应该这样写
>
> def f1():
>      a = [0]
>      def f2():
>          a[0] += 1
>          return a[0]
>      return f2
>
>

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号