Python Weak Ref
FrontPage|FindPage|TitleIndex|RecentChanges|UserPreferences E D R S I M H RSS

python.or.kr ¿¡¼­ 2.1 ¿¡¼­ ´Þ¶óÁøÁ¡ ¹®¼­¿¡¼­ °¡Á®¿È

2. ¾àÇÑ ÂüÁ¶(Weak Reference)ÀÇ Áö¿ø ¾àÇÑÂüÁ¶(weak reference)¶õ ¹«¾ùÀΰ¡? weak reference¶õ ·¹ÆÛ·±½º Ä«¿îÆ®·Î ¼ÀµÇÁö ¾Ê´Â ·¹ÆÛ·±½ºÀÌ´Ù. °´Ã¼¿¡ ¾àÇÑ ÂüÁ¶¸¦ Á¦¿ÜÇÏ°í ´Ù¸¥ ·¹ÆÛ·±½º°¡ Á¸ÀçÇÏÁö ¾Ê°ÔµÇ¸é, ±× °´Ã¼´Â ÇØÁ¦(release)µÇ°í ¸ðµç ¾àÇÑ ÂüÁ¶´Â Á¤ÀǵÈÁö ¾ÊÀº »óÅ·Π¹Ù²ï´Ù.

¾àÇÑÂüÁ¶(weak reference)´Â ¿Ö ÇÊ¿äÇÑ°¡? ÀÌ·¯ÇÑ ³»¿ëÀº ¼øȯÀûÀÎ ÂüÁ¶¿¡ »ç¿ëµÉ ¼ö ÀÖ´Ù. ¼øȯ ÂüÁ¶´Â ¾²·¹±â ¼öÁýÀ» ¹æÇØÇÏ´Â ÁÖ¿äÇÑ ¹®Á¦¿´´Ù. 2.0¿¡¼­´Â ÁÖ±âÀûÀ¸·Î ¼øȯÂüÁ¶¸¦ °Ë»çÇؼ­ ¾²·¹±â ¼öÁýÀ» ÇÏ´Â ±â´ÉÀÌ Ãß°¡µÇ¾úÁö¸¸, ÀÚÁÖ ÇÏ°ÔµÇ¸é ¾öû³­ ¾µµ¥¾ø´Â CPUÀÚ¿øÀ» ³¶ºñÇÏ´Â °ÍÀÌ°í ÀÚÁÖ ÇÏÁö ¾Ê°Ô µÇ¸é ¸¹Àº ÇØÁ¦µÇ¾î¾ßÇÒ ¼øȯ ÂüÁ¶µÇ´Â °´Ã¼µéÀÌ ¸Þ¸ð¸®¿¡ Á¸ÀçÇÒ ¼ö ÀÖ´Ù. ½ÇÁ¦·Î DOM ÀÀ¿ë¿¡¼­´Â ¼øȯÂüÁ¶°¡ ¹«¼öÈ÷ ¹ß»ýÇÑ´Ù°í ÇÑ´Ù. ÀÌ°ÍÀ» ÇØ°áÇϱâ À§ÇÑ °ÍÀÌ ¾àÇÑÂüÁ¶(weak reference)ÀÌ´Ù. ¶ÇÇÑ, ¾àÇÑ ÂüÁ¶´Â ´Ù¾çÇÑ ÀνºÅϽºµé »çÀÌ¿¡¼­ °øÀ¯µÇ´Â °´Ã¼¸¦ ÂüÁ¶Çϴµ¥µµ »ç¿ëµÉ ¼ö ÀÖ´Ù.

¾àÇÑ »çÀü(Weak dictionary)À̶õ? ij½¬(Caches)¶ó°íµµ ÇÑ´Ù. ¾àÇÑ »çÀüÀº °ªÀ¸·Î ¾àÇÑ ÂüÁ¶¸¦ °¡Áö°í ÀÖ´Â »çÀüÀ» ¸»ÇÑ´Ù. ÀϹÝÀûÀÎ »çÀü°ú °°Áö¸¸ ´ÙÀ½°ú °°Àº Â÷ÀÌÁ¡ÀÌ ÀÖ´Ù.

°ªÀ¸·Î ¾àÇÑ ÂüÁ¶°¡ °¡´ÉÇÑ °´Ã¼¸¦ ¹Þ¾ÆµéÀδÙ. °ªÀ¸·Î ġȯµÈ ³»¿ëÀº ¾àÇÑ ÂüÁ¶µÈ´Ù. °´Ã¼°¡ »èÁ¦µÇ¸é ÀÚµ¿ÀûÀ¸·Î ij½¬¿¡ ÀÖ´Â Å°,°ª ½Öµµ »èÁ¦µÈ´Ù.

±×·³ ¾àÇÑ ÂüÁ¶°¡ °¡´ÉÇÑ °´Ã¼´Â ¾î¶² °ÍÀΰ¡? ¸ðµç °´Ã¼¿¡ ´ëÇؼ­ ¾àÇÑ ÂüÁ¶°¡ °¡´ÉÇÏÁö´Â ¾Ê´Ù. ½ÇÁ¦ÀûÀÎ ¸Þ¸ð¸® Å©±â¸¦ °®°í Àְųª, ¿ÜºÎ ÀÚ¿øÀ¸·Î ÂüÁ¶(µ¥ÀÌÅÍ º£À̽º ¿¬°á, ¿ÀÇ ÆÄÀÏ °´Ã¼µî)¸¦ °¡Áö°í ÀÖ´Â °æ¿ì°¡ °¡´ÉÇÏ´Ù. ¿¹¸¦ µé¸é, ..

.... (¿¹ Ãß°¡.)

¾àÇÑ ÂüÁ¶ ¸ðµâ weakref weakref.ref()´Â ¾àÇÑ ÂüÁ¶ °´Ã¼¸¦ »ý¼ºÇÑ´Ù. ¾àÇÑ ÂüÁ¶´Â ¾²·¹±â ¼öÁýµÇÁö ¾ÊÀº °´Ã¼¸¦ ÂüÁ¶ÇÏ°í, ½ÇÁ¦·Î ±× °´Ã¼°¡ ¸Þ¸ð¸®¿¡ ÀÖ´ÂÁö Á¶»çÇϴµ¥µµ »ç¿ëµÈ´Ù. ·¹ÆÛ·±½º °´Ã¼¸¦ È£ÃâÇÏ¿© ·¹ÆÛ·±½º¸¦ °Ë»ö(retrieve)ÇÑ´Ù. ·¹ÆÛ·±½º°¡ ´õ ÀÌ»ó Á¸ÀçÇÏÁö ¾ÊÀ¸¸é, NoneÀ» ´ë½Å ¸®ÅÏÇÑ´Ù. weakref.mapping()Àº ¾àÇÑ »çÀüÀ» »ý¼ºÇÑ´Ù.

ÀÓÀÇÀÇ Å°¸¦ °ª¿¡ ¸ÅÇÎÇÑ´Ù. ±×·¯³ª, °ª¿¡ ´ëÇÑ ÂüÁ¶¸¦ ¼ÒÀ¯ÇÏÁö ¾Ê´Â´Ù. ±× °ªÀÌ »ç¶óÁö°Ô µÇ¸é, (key, value) ½ÖÀº ¸ðµç »çÀü¿¡¼­ Á¦°ÅµÈ´Ù. »çÀü°ú °°ÀÌ ¾àÇÑ »çÀüÀº Çؽ¬°¡´ÉÇÏÁö ¾Ê´Ù. weakref.proxy()´Â ¿ø·¡ÀÇ °´Ã¼¿Í À¯»çÇÏ°Ô µ¿ÀÛÇÏ´Â ÇÁ·Ï½Ã °´Ã¼¸¦ »ý¼ºÇÑ´Ù.

ÇÁ·Ï½Ã °´Ã¼¶õ, ¿ø·¡ÀÇ °´Ã¼¿Í À¯»çÇÏ°Ô µ¿ÀÛÇÏ·Á°í ÇÏ´Â ¾àÇÑ ÂüÁ¶ÀÌ´Ù. Çؽ¬ °¡´ÉÇÏÁö ¾Ê´Ù. ÂüÁ¶°¡ ¾ø¾îÁø ÈÄ¿¡ ¿¬»êÀ» ¼öÇàÇϸé weakref.?ReferenceError ¿¹¿Ü°¡ ¹ß»ýÇÑ´Ù. "is", type() ¹× id()¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù.

½ÇÁ¦ÀûÀÎ ÄÚµå´Â µÇÁö ¸øÇÏÁö¸¸, µ¿ÀÛ Æ¯¼ºÀ» º¼ ¼ö ÀÖµµ·Ï ¿¹¸¦ ´ÙÀ½¿¡ º¸ÀδÙ.

weakref.ref ¿¹Á¦:
>>> import sys 
>>> import weakref 
>>> class Foo:pass 
  
>>> f = Foo() 
>>> r = weakref.ref(f) 
>>> r 
<weakref at 0xbb59fc; to 'instance' at 0xbae09c> 
>>> r() 
<__main__.Foo instance at 00BAE09C> 
>>> f 
<__main__.Foo instance at 00BAE09C> 
>>> sys.getrefcount(f) # ·¹ÆÛ·±½º Ä«¿îÆ® °ª Àо±â 
2    # ½ÇÁ¦·Î´Â 1ÀÌ´Ù. getrefcount ÇÔ¼ö·Î ÀÎÇØ 1ÀÌ Ãß°¡µÇ¾îÀֱ⠶§¹®ÀÌ´Ù. 
>>> g = f 
>>> sys.getrefcount(f) 
3 
>>> del g 
>>> del f 
>>> r 
<weakref at bb59fc; dead> 
>>> r() 
weakref.mapping ¿¹Á¦:
>>> f = Foo() 
>>> sys.getrefcount(f) 
2 
>>> d = weakref.mapping()    # weakref¿ë »çÀüÀ» ¾òÀ½ 
>>> sys.getrefcount(f) 
2 
>>> d['f'] = f        # weak reference °¡´ÉÇÑ °´Ã¼¸¦ °ªÀ¸·Î ³Ñ°Ü¾ßÇÔ. 
>>> sys.getrefcount(f)    # ·¹ÆÛ·±½º Ä«¿îÆ® º¯È­ ¾øÀ½. 
2 
>>> f = Foo() 
>>> d = weakref.mapping() 
>>> d['f'] = f 
>>> d['f'] 
<__main__.Foo instance at 01259FDC> 
>>> f 
<__main__.Foo instance at 01259FDC> 
>>> del f 
>>> len(d) 
1 
>>> d.items() 
[] 
>>> len(d) 
0 
weakref.proxy ¿¹Á¦:

.........................

EditText|FindPage|DeletePage|LikePages| Valid XHTML 1.0! Valid CSS! powered by MoniWiki