[python-chinese] 最近在研究python源码,有个问题搞不明白,求教!

Robert Chen search.pythoner在gmail.com
星期三 十一月 29 15:53:03 HKT 2006


ºÇºÇ£¬Ô­ÒòºÜ¼òµ¥£¬ÔÚfooÖ®ÍâµÄframeÖУ¬localºÍglobalÊÇͬһ¸ödict¡£
x = 1
def foo():
    print '================= local %d =================\n' % id(locals()),
locals()
    print '================= global %d =================\n' % id(globals()),
globals()
    y = x+1
    print x
    print y

print '================= local %d =================\n' % id(locals()),
locals()
print '================= global %d =================\n' % id(globals()),
globals()
foo()
print x

´ÓÊä³öµÄ½á¹ûÖпÉÒÔ¿´µ½ÕâÒ»µã£¬ËùÒÔfooÖ®ÍâÏòlocalÖÐÉèÖÃÖµ¾ÍÊÇÏòglobalÖÐÉèÖÃÖµ¡£

On 11/29/06, libing <imlibing在163.com> wrote:
>
>    3           0 LOAD_GLOBAL              0 (x)
> ÕâÌõÊÇfoo´ÓglobalÀï²éxµÄ£¬globalÓÖÊÇ´ÓfooÍâÃæ´«½øÀ´µÄ£¬µ«ÊÇÎÊÌâÊÇÔÚfooÍâÃæÃ»Óп´µ½ÍùglobalÉèxÖµµÄ´úÂ룬ÓеÄÖ»ÊÇ
>               3 STORE_NAME               0 (x)
> ÕâÌõ£¬ÕâÊÇÍùfooÍâÃæµÄlocal²åÊý¾ÝµÄ
>
> ----- Original Message -----
> *From:* ÁõöÎ <march.liu在gmail.com>
> *To:* python-chinese在lists.python.cn
> *Sent:* Wednesday, November 29, 2006 11:03 AM
> *Subject:* Re: [python-chinese]×î½üÔÚÑо¿pythonÔ´Â룬ÓиöÎÊÌâ¸ã²»Ã÷°×£¬Çó½Ì£¡
>
> Ó¦¸ÃÊÇÕâÌõ
>   3           0 LOAD_GLOBAL              0 (x)
> ÎÒ²ÂÏëÊǽâÊÍÆ÷±àÒëfooµÄʱºò£¬ÔÚfooÄÚ²¿ÕÒ²»µ½xµÄ¸³Öµ£¬¾ÍÖ±½ÓÔÚ×îÇ°Ãæ²åÈëÁËÕâ¸öÖ¸Áî¡£
>
> 2006/11/29, libing <imlibing在163.com>:
> >
> > hello,´ó¼ÒºÃ£¬Ð¡µÜ×î½üÔÚÑо¿pythonÔ´Â룬ÓиöÎÊÌâ¸ã²»Ã÷°×£¬Çó½Ì£¡
> >
> > ¶ÔÒ»¶Î´úÂ룺
> > x = 1
> > def foo():
> > y = x+1
> > print x
> > print y
> > foo()
> > print x
> >
> > ÎÒ´òÓ¡³öËüµÄcode object ½á¹¹³öÀ´·ÖÎö£º
> > £½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½
> > co_names : ('x', 'foo')
> > co_varnames : ()
> > co_freevars : ()
> > co_cellvars : ()
> >   1           0 LOAD_CONST               0 (1)
> >               3 STORE_NAME               0 (x)
> >
> >   2           6 LOAD_CONST               1 (<code object foo at
> > 010167B8, file "string", line 2>)
> >
> > nest code object begin <<<<<
> > co_names : ('x',)
> > co_varnames : ('y',)
> > co_freevars : ()
> > co_cellvars : ()
> >   3           0 LOAD_GLOBAL              0 (x)
> >               3 LOAD_CONST               1 (1)
> >               6 BINARY_ADD
> >               7 STORE_FAST               0 (y)
> >
> >   4          10 LOAD_GLOBAL              0 (x)
> >              13 PRINT_ITEM
> >              14 PRINT_NEWLINE
> >
> >   5          15 LOAD_FAST                0 (y)
> >              18 PRINT_ITEM
> >              19 PRINT_NEWLINE
> >              20 LOAD_CONST               0 (None)
> >              23 RETURN_VALUE
> > nest code object end >>>>>
> >
> >               9 MAKE_FUNCTION            0
> >              12 STORE_NAME               1 (foo)
> >
> >   6          15 LOAD_NAME                1 (foo)
> >              18 CALL_FUNCTION            0
> >              21 POP_TOP
> >
> >   7          22 LOAD_NAME                0 (x)
> >              25 PRINT_ITEM
> >              26 PRINT_NEWLINE
> >              27 LOAD_CONST               2 (None)
> >              30 RETURN_VALUE
> > £½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½
> >
> > ÔÚÍâ²ã´æxÖµµÄʱºòµÄbyte codeÊÇ              3 STORE_NAME               0 (x)
> > ÔÚceval.cÔ´ÂëÀïÕâ¶Î¶ÔÓ¦µÄÊÇ
> >   case STORE_NAME:
> >    w = GETITEM(names, oparg);
> >    v = POP();
> >    if ((x = f->f_locals) != NULL) {
> >     if (PyDict_CheckExact(x))
> >      err = PyDict_SetItem(x, w, v);
> >     else
> >      err = PyObject_SetItem(x, w, v);
> >     Py_DECREF(v);
> >     if (err == 0) continue;
> >     break;
> >    }
> >    PyErr_Format(PyExc_SystemError,
> >          "no locals found when storing %s",
> >          PyObject_REPR(w));
> >    break;
> >
> > ÕâÊÇ´æÔÚfooÍâ²ãframeµÄµÄlocalÀï°É£¿µ«ÊÇÍâ²ãbyte codeÀïxûÓÐÔÚco_varnamesÀïÃæ£¬ÄÇôËü¾ÍÓ¦¸ÃÔÚglobalÀï°É£¿
> > fooÀïÃæÓÖÈçºÎ´ÓÍâ²ã´«½øÀ´µÄglobal±íÀï²éµ½xµÄֵĨ£¿ÎÒÔõôҲÕÒ²»µ½fooÍâÃæ½«x¸³Öµµ½global±íµÄ²¿·Ö£¬Çó½ÌÁË£¡
> > _______________________________________________
> > 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
>
>
>
>
> --
> »¶Ó­·ÃÎÊ£º
> http://blog.csdn.net/ccat
>
> ÁõöÎ
> March.Liu
>
> ------------------------------
>
> _______________________________________________
> 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
>



-- 
Robert
PythonÔ´ÂëÆÊÎö¡ª¡ªhttp://blog.donews.com/lemur/
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061129/f5d6bb9f/attachment.html 


关于邮件列表 python-chinese 的更多信息