μΉ΄ν…Œκ³ λ¦¬ 보관물: Python

Python

파이썬 쀑첩 ν•¨μˆ˜κ°€ ν΄λ‘œμ €λΌκ³  λΆˆλ¦¬λŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? μ•„λ‹Œκ°€? μ—…λ°μ΄νŠΈ : ν΄λ‘œμ €μ— λŒ€ν•΄

νŒŒμ΄μ¬μ—μ„œ 쀑첩 ν•¨μˆ˜λ₯Όλ³΄κ³  μ‚¬μš©ν–ˆμœΌλ©° ν΄λ‘œμ € μ •μ˜μ™€ μΌμΉ˜ν•©λ‹ˆλ‹€. μ™œ nested functionsλŒ€μ‹ μ— 그듀은 λΆˆλ €μ§€ closuresλŠ”κ°€?

쀑첩 ν•¨μˆ˜λŠ” μ™ΈλΆ€ μ„Έκ³„μ—μ„œ μ‚¬μš©λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— ν΄λ‘œμ €κ°€ μ•„λ‹Œκ°€?

μ—…λ°μ΄νŠΈ : ν΄λ‘œμ €μ— λŒ€ν•΄ 읽고 μžˆμ—ˆκ³  파이썬과 κ΄€λ ¨ ν•˜μ—¬μ΄ κ°œλ…μ— λŒ€ν•΄ μƒκ°ν•˜κ²Œν–ˆμŠ΅λ‹ˆλ‹€. μ•„λž˜ μ£Όμ„μ—μ„œ λˆ„κ΅°κ°€κ°€ μ–ΈκΈ‰ ν•œ 기사λ₯Ό κ²€μƒ‰ν•˜κ³  μ°Ύμ•˜μ§€λ§Œ ν•΄λ‹Ή κΈ°μ‚¬μ˜ μ„€λͺ…을 μ™„μ „νžˆ 이해할 수 μ—†μœΌλ―€λ‘œμ΄ μ§ˆλ¬Έμ„ν•˜λŠ” μ΄μœ μž…λ‹ˆλ‹€.



λ‹΅λ³€

νμ‡„λŠ” ν•¨μˆ˜κ°€ 싀행을 μ™„λ£Œ ν•œ λ‘˜λŸ¬μ‹ΈλŠ” λ²”μœ„μ—μ„œ 둜컬 λ³€μˆ˜μ— μ•‘μ„ΈμŠ€ ν•  λ•Œ λ°œμƒν•©λ‹ˆλ‹€.

def make_printer(msg):
    def printer():
        print msg
    return printer

printer = make_printer('Foo!')
printer()

make_printer호좜 되면 printerν•¨μˆ˜μ— λŒ€ν•œ 컴파일 된 μ½”λ“œ κ°€ μƒμˆ˜ 및 msg둜컬 κ°’μœΌλ‘œ μŠ€νƒμ— μƒˆ ν”„λ ˆμž„μ΄ λ°°μΉ˜λ©λ‹ˆλ‹€ . 그런 λ‹€μŒ ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜κ³  λ¦¬ν„΄ν•©λ‹ˆλ‹€. ν•¨μˆ˜ printerλŠ” msgλ³€μˆ˜λ₯Ό μ°Έμ‘° ν•˜λ―€λ‘œ make_printerν•¨μˆ˜κ°€ λ°˜ν™˜ 된 후에도 계속 μœ μ§€λ©λ‹ˆλ‹€ .

λ”°λΌμ„œ 쀑첩 ν•¨μˆ˜κ°€ κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄

  1. λ‘˜λŸ¬μ‹ΈλŠ” λ²”μœ„μ— 둜컬 인 λ³€μˆ˜μ— μ•‘μ„ΈμŠ€
  2. 그것듀이 κ·Έ λ²”μœ„ λ°–μ—μ„œ 싀행될 λ•Œ κ·Έλ ‡κ²Œν•˜μ‹­μ‹œμ˜€.

그듀은 νμ‡„λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‹€μŒμ€ ν΄λ‘œμ €κ°€ μ•„λ‹Œ 쀑첩 ν•¨μˆ˜μ˜ μ˜ˆμž…λ‹ˆλ‹€.

def make_printer(msg):
    def printer(msg=msg):
        print msg
    return printer

printer = make_printer("Foo!")
printer()  #Output: Foo!

μ—¬κΈ°μ„œλŠ” 값을 맀개 λ³€μˆ˜μ˜ 기본값에 λ°”μΈλ”©ν•©λ‹ˆλ‹€. 이것은 ν•¨μˆ˜ printerκ°€ μž‘μ„± 될 λ•Œ λ°œμƒ ν•˜λ―€λ‘œ 리턴 후에 msgexternal 값에 λŒ€ν•œ μ°Έμ‘°λ₯Ό printerμœ μ§€ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€ make_printer. 이 λ¬Έλ§₯ msgμ—μ„œ ν•¨μˆ˜μ˜ 일반적인 μ§€μ—­ λ³€μˆ˜ 일뿐 printerμž…λ‹ˆλ‹€.


λ‹΅λ³€

이 μ§ˆλ¬Έμ€ 이미 aaronasterling λ‹˜ 에 μ˜ν•΄ λ‹΅λ³€λ˜μ—ˆμŠ΅λ‹ˆλ‹€

κ·ΈλŸ¬λ‚˜ λˆ„κ΅°κ°€ λ³€μˆ˜κ°€ ν›„λ“œ μ•„λž˜μ— μ €μž₯λ˜λŠ” 방식에 κ΄€μ‹¬μ΄μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

슀 λ‹ˆνŽ«μ— 였기 전에 :

ν΄λ‘œμ €λŠ” μ—” ν΄λ‘œμ§• ν™˜κ²½μ—μ„œ λ³€μˆ˜λ₯Ό μƒμ†ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. ν•¨μˆ˜ μ½œλ°±μ„ I / Oλ₯Ό μˆ˜ν–‰ν•˜λŠ” λ‹€λ₯Έ ν•¨μˆ˜μ— λŒ€ν•œ 인수둜 μ „λ‹¬ν•˜λ©΄μ΄ 콜백 ν•¨μˆ˜κ°€ λ‚˜μ€‘μ— 호좜되며이 ν•¨μˆ˜λŠ” 거의 마술처럼 μ‚¬μš© κ°€λŠ₯ν•œ λͺ¨λ“  λ³€μˆ˜μ™€ ν•¨κ»˜ μ„ μ–Έ 된 μ»¨ν…μŠ€νŠΈλ₯Ό κΈ°μ–΅ν•©λ‹ˆλ‹€. κ·Έ λ§₯λ½μ—μ„œ.

  • ν•¨μˆ˜κ°€ 자유 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ©΄ ν΄λ‘œμ €λ₯Ό ν˜•μ„±ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

  • 자유 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” λ‹€λ₯Έ λ‚΄λΆ€ λ ˆλ²¨μ΄μžˆλŠ” β€‹β€‹κ²½μš° λͺ¨λ“  이전 λ ˆλ²¨μ€ μ–΄νœ˜ ν™˜κ²½μ„ μ €μž₯ν•©λ‹ˆλ‹€ (예 : 끝에)

  • κΈ°λŠ₯ 속성 func_closure에 파이썬 <3.x λ˜λŠ” __closure__자유 λ³€μˆ˜ μ €μž₯ 파이썬> 3.X.

  • 파이썬의 λͺ¨λ“  ν•¨μˆ˜μ—λŠ”μ΄ 폐쇄 속성이 μžˆμ§€λ§Œ 자유 λ³€μˆ˜κ°€ μ—†μœΌλ©΄ λ‚΄μš©μ„ μ €μž₯ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

예 : 자유 λ³€μˆ˜κ°€ μ—†μœΌλ―€λ‘œ ν΄λ‘œμ € 속성이 μžˆμ§€λ§Œ λ‚΄μš©μ΄ μ—†μŠ΅λ‹ˆλ‹€.

>>> def foo():
...     def fii():
...         pass
...     return fii
...
>>> f = foo()
>>> f.func_closure
>>> 'func_closure' in dir(f)
True
>>>

주의 : 무료 λ³€μˆ˜ λŠ” 폐쇄λ₯Ό λ§Œλ“€μ–΄μ•Όν•©λ‹ˆλ‹€.

μœ„μ™€ λ™μΌν•œ 슀 λ‹ˆνŽ«μ„ μ‚¬μš©ν•˜μ—¬ μ„€λͺ…ν•©λ‹ˆλ‹€.

>>> def make_printer(msg):
...     def printer():
...         print msg
...     return printer
...
>>> printer = make_printer('Foo!')
>>> printer()  #Output: Foo!

그리고 λͺ¨λ“  파이썬 ν•¨μˆ˜μ—λŠ” ν΄λ‘œμ € 속성이 μžˆμœΌλ―€λ‘œ ν΄λ‘œμ € ν•¨μˆ˜μ™€ κ΄€λ ¨λœ λ‘˜λŸ¬μ‹ΈλŠ” λ³€μˆ˜λ₯Ό μ‚΄νŽ΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

func_closureν•¨μˆ˜ 의 속성은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€printer

>>> 'func_closure' in dir(printer)
True
>>> printer.func_closure
(<cell at 0x108154c90: str object at 0x108151de0>,)
>>>

이 closure속성은 λ‘˜λŸ¬μ‹ΈλŠ” λ²”μœ„μ— μ •μ˜ 된 λ³€μˆ˜μ˜ μ„ΈλΆ€ 사항을 ν¬ν•¨ν•˜λŠ” μ…€ 였브젝트의 νŠœν”Œμ„ λ¦¬ν„΄ν•©λ‹ˆλ‹€.

func_closure의 첫 번째 μš”μ†ŒλŠ” ν•¨μˆ˜μ˜ 자유 λ³€μˆ˜μ— λŒ€ν•œ 바인딩을 ν¬ν•¨ν•˜λŠ” None λ˜λŠ” μ…€ νŠœν”Œ 일 수 있으며 읽기 μ „μš©μž…λ‹ˆλ‹€.

>>> dir(printer.func_closure[0])
['__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__getattribute__',
 '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
 '__setattr__',  '__sizeof__', '__str__', '__subclasshook__', 'cell_contents']
>>>

μœ„μ˜ 좜λ ₯ cell_contentsμ—μ„œ λ³Ό μˆ˜μžˆλŠ” λ‚΄μš© 을 보자.

>>> printer.func_closure[0].cell_contents
'Foo!'
>>> type(printer.func_closure[0].cell_contents)
<type 'str'>
>>>

λ”°λΌμ„œ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ©΄μ— printer()μ €μž₯된 값에 μ•‘μ„ΈμŠ€ν•©λ‹ˆλ‹€ cell_contents. 이것이 좜λ ₯을 β€˜Foo!β€™λ‘œ 얻은 λ°©λ²•μž…λ‹ˆλ‹€.

λ‹€μ‹œ μœ„μ˜ 슀 λ‹ˆνŽ«μ„ μ‚¬μš©ν•˜μ—¬ λͺ‡ κ°€μ§€ λ³€κ²½ 사항을 μ„€λͺ…ν•©λ‹ˆλ‹€.

 >>> def make_printer(msg):
 ...     def printer():
 ...         pass
 ...     return printer
 ...
 >>> printer = make_printer('Foo!')
 >>> printer.func_closure
 >>>

μœ„μ˜ 슀 λ‹ˆνŽ«μ—μ„œλŠ” ν”„λ¦°ν„° ν•¨μˆ˜ 내뢀에 msgλ₯Ό μΈμ‡„ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ μ‚¬μš© κ°€λŠ₯ν•œ λ³€μˆ˜λ₯Ό λ§Œλ“€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 자유 λ³€μˆ˜κ°€ μ—†μœΌλ―€λ‘œ ν΄λ‘œμ € μ•ˆμ— λ‚΄μš©μ΄ μ—†μŠ΅λ‹ˆλ‹€. 그것이 λ°”λ‘œ μš°λ¦¬κ°€ μœ„μ—μ„œ λ³Έ κ²ƒμž…λ‹ˆλ‹€.

μ§€κΈˆμ€ λͺ¨λ“  것을 μ·¨μ†Œ λ‹€λ₯Έ λ‹€λ₯Έ 쑰각을 μ„€λͺ… ν•  것 Free Variable으둜 Closure:

>>> def outer(x):
...     def intermediate(y):
...         free = 'free'
...         def inner(z):
...             return '%s %s %s %s' %  (x, y, free, z)
...         return inner
...     return intermediate
...
>>> outer('I')('am')('variable')
'I am free variable'
>>>
>>> inter = outer('I')
>>> inter.func_closure
(<cell at 0x10c989130: str object at 0x10c831b98>,)
>>> inter.func_closure[0].cell_contents
'I'
>>> inn = inter('am')

κ·Έλž˜μ„œ μš°λ¦¬λŠ” func_closure속성이 ν΄λ‘œμ € μ…€ 의 νŠœν”Œμ΄λΌλŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” 속성과 κ·Έ λ‚΄μš©μ„ λͺ…μ‹œ 적으둜 μ°Έμ‘° ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ…€μ—λŠ” β€œcell_contents”속성이 μžˆμŠ΅λ‹ˆλ‹€

>>> inn.func_closure
(<cell at 0x10c9807c0: str object at 0x10c9b0990>,
 <cell at 0x10c980f68: str object at   0x10c9eaf30>,
 <cell at 0x10c989130: str object at 0x10c831b98>)
>>> for i in inn.func_closure:
...     print i.cell_contents
...
free
am
I
>>>

μ—¬κΈ°μ„œ 호좜 innν•˜λ©΄ λͺ¨λ“  μ €μž₯ 무료 λ³€μˆ˜λ₯Ό μ°Έμ‘°ν•˜λ―€λ‘œI am free variable

>>> inn('variable')
'I am free variable'
>>>

λ‹΅λ³€

νŒŒμ΄μ¬μ€ ν΄λ‘œμ €λ₯Ό μ•½ν•˜κ²Œ μ§€μ›ν•©λ‹ˆλ‹€. 무슨 λœ»μΈμ§€ ν™•μΈν•˜λ €λ©΄ JavaScript둜 ν΄λ‘œμ €λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μŒ μΉ΄μš΄ν„° 예제λ₯Ό μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

function initCounter(){
    var x = 0;
    function counter  () {
        x += 1;
        console.log(x);
    };
    return counter;
}

count = initCounter();

count(); //Prints 1
count(); //Prints 2
count(); //Prints 3

ν΄λ‘œμ €λŠ” 이와 같이 μž‘μ„±λœ ν•¨μˆ˜μ— β€œλ‚΄λΆ€ λ©”λͺ¨λ¦¬β€κΈ°λŠ₯을 μ œκ³΅ν•˜κΈ° λ•Œλ¬Έμ— 맀우 μš°μ•„ν•©λ‹ˆλ‹€. 파이썬 2.7λΆ€ν„°λŠ” λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€. 당신이 μ‹œλ„ν•˜λ©΄

def initCounter():
    x = 0;
    def counter ():
        x += 1 ##Error, x not defined
        print x
    return counter

count = initCounter();

count(); ##Error
count();
count();

xκ°€ μ •μ˜λ˜μ§€ μ•Šμ•˜λ‹€λŠ” 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ λ‹€λ₯Έ μ‚¬λžŒλ“€μ΄ 인쇄 ν•  수 μžˆλ‹€λŠ” 것을 μ–΄λ–»κ²Œ μ•Œ 수 μžˆμŠ΅λ‹ˆκΉŒ? 이것은 파이썬이 ν•¨μˆ˜ λ³€μˆ˜ λ²”μœ„λ₯Ό κ΄€λ¦¬ν•˜λŠ” 방법 λ•Œλ¬Έμž…λ‹ˆλ‹€. λ‚΄λΆ€ ν•¨μˆ˜λŠ” μ™ΈλΆ€ ν•¨μˆ˜μ˜ λ³€μˆ˜λ₯Ό 읽을 수 μžˆμ§€λ§Œ μ“Έ μˆ˜λŠ” μ—†μŠ΅λ‹ˆλ‹€ .

정말 λΆ€λ„λŸ¬μš΄ μΌμž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 읽기 μ „μš© ν΄λ‘œμ €λ₯Ό μ‚¬μš©ν•˜λ©΄ Pythonμ—μ„œ ꡬ문 섀탕을 제곡 ν•˜λŠ” ν•¨μˆ˜ λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄ 을 적어도 κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€ .

μ΅œμ‹  정보

μ§€μ ν–ˆλ“―μ΄ 파이썬의 λ²”μœ„ μ œν•œμ„ μ²˜λ¦¬ν•˜λŠ” 방법이 있으며 λͺ‡ κ°€μ§€λ₯Ό κ³΅κ°œν•˜κ² μŠ΅λ‹ˆλ‹€.

1.global ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ‹­μ‹œμ˜€ (일반적으둜 ꢌμž₯ν•˜μ§€ μ•ŠμŒ).

2. Python 3.xμ—μ„œ nonlocalν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ‹­μ‹œμ˜€ (@unutbu 및 @leewzμ—μ„œ μ œμ•ˆ).

3. κ°„λ‹¨ν•œ μˆ˜μ • κ°€λŠ₯ν•œ 클래슀 μ •μ˜Object

class Object(object):
    pass

λ³€μˆ˜λ₯Ό μ €μž₯ν•  Object scopeλ‚΄λΆ€ initCounterλ₯Ό μž‘μ„± ν•˜μ‹­μ‹œμ˜€.

def initCounter ():
    scope = Object()
    scope.x = 0
    def counter():
        scope.x += 1
        print scope.x

    return counter

scopeμ‹€μ œλ‘œλŠ” μ°Έμ‘° 일 뿐이 λ―€λ‘œ ν•΄λ‹Ή ν•„λ“œλ‘œ μˆ˜ν–‰ 된 μž‘μ—…μ€ μ‹€μ œλ‘œ μˆ˜μ • scopeλ˜μ§€ μ•ŠμœΌλ―€λ‘œ 였λ₯˜κ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

4. @unutbuκ°€ μ§€μ ν–ˆλ“―μ΄ λŒ€μ•ˆμ€ 각 λ³€μˆ˜λ₯Ό λ°°μ—΄ ( x = [0]) 둜 μ •μ˜ ν•˜κ³  첫 번째 μš”μ†Œ ( x[0] += 1)λ₯Ό μˆ˜μ •ν•˜λŠ” 것 μž…λ‹ˆλ‹€. x자체 μˆ˜μ •λ˜μ§€ μ•Šμ•˜κΈ° λ•Œλ¬Έμ— 였λ₯˜κ°€ λ‹€μ‹œ λ°œμƒ ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

5. @raxacoricofallapatorius에 μ˜ν•΄ μ œμ•ˆμœΌλ‘œ, 당신은 λ§Œλ“€ 수 x의 속성을counter

def initCounter ():

    def counter():
        counter.x += 1
        print counter.x

    counter.x = 0
    return counter

λ‹΅λ³€

파이썬 2μ—λŠ” ν΄λ‘œμ €κ°€ μ—†μ—ˆμŠ΅λ‹ˆλ‹€ . ν΄λ‘œμ € 와 λΉ„μŠ·ν•œ ν•΄κ²° λ°©λ²•μ΄μžˆμ—ˆμŠ΅λ‹ˆλ‹€ .

이미 μ£Όμ–΄μ§„ λ‹΅λ³€μ—λŠ” λ‚΄λΆ€ ν•¨μˆ˜μ— λ³€μˆ˜λ₯Ό λ³΅μ‚¬ν•˜κ±°λ‚˜ λ‚΄λΆ€ ν•¨μˆ˜μ˜ 객체λ₯Ό μˆ˜μ •ν•˜λŠ” λ“±μ˜ λ§Žμ€ μ˜ˆμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

Python 3μ—μ„œ 지원은보닀 λͺ…ν™•ν•˜κ³  κ°„κ²°ν•©λ‹ˆλ‹€.

def closure():
    count = 0
    def inner():
        nonlocal count
        count += 1
        print(count)
    return inner

μš©λ²•:

start = closure()
start() # prints 1
start() # prints 2
start() # prints 3

nonlocalν‚€μ›Œλ“œλ₯Ό κ°μ‹ΈλŠ” νš¨κ³Όμ— λͺ…μ‹œ 적 μ–ΈκΈ‰ μ™ΈλΆ€ λ³€μˆ˜ λ‚΄λΆ€ κΈ°λŠ₯을 κ²°ν•©ν•œλ‹€. λ”°λΌμ„œλ³΄λ‹€ λͺ…ν™•ν•˜κ²Œ β€˜νμ‡„β€™μž…λ‹ˆλ‹€.


λ‹΅λ³€

λ³„λ„μ˜ 영ꡬ λ„€μž„ μŠ€νŽ˜μ΄μŠ€κ°€ ν•„μš”ν•œ μƒν™©μ΄μžˆμ—ˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” μˆ˜μ—…μ„ μ‚¬μš©ν–ˆλ‹€. λ‚˜λŠ” κ·Έλ ‡μ§€ μ•Šλ‹€. λΆ„λ¦¬λ˜μ—ˆμ§€λ§Œ 영ꡬ적 인 이름은 νμ‡„μž…λ‹ˆλ‹€.

>>> class f2:
...     def __init__(self):
...         self.a = 0
...     def __call__(self, arg):
...         self.a += arg
...         return(self.a)
...
>>> f=f2()
>>> f(2)
2
>>> f(2)
4
>>> f(4)
8
>>> f(8)
16

# **OR**
>>> f=f2() # **re-initialize**
>>> f(f(f(f(2)))) # **nested**
16

# handy in list comprehensions to accumulate values
>>> [f(i) for f in [f2()] for i in [2,2,4,8]][-1]
16

λ‹΅λ³€

def nested1(num1):
    print "nested1 has",num1
    def nested2(num2):
        print "nested2 has",num2,"and it can reach to",num1
        return num1+num2    #num1 referenced for reading here
    return nested2

μ œκ³΅ν•©λ‹ˆλ‹€ :

In [17]: my_func=nested1(8)
nested1 has 8

In [21]: my_func(5)
nested2 has 5 and it can reach to 8
Out[21]: 13

이것은 ν΄λ‘œμ €κ°€ 무엇이며 μ–΄λ–»κ²Œ μ‚¬μš©λ˜λŠ”μ§€μ— λŒ€ν•œ μ˜ˆμž…λ‹ˆλ‹€.


λ‹΅λ³€

파이썬과 JS 예제 사이에 또 ​​닀λ₯Έ κ°„λ‹¨ν•œ 비ꡐλ₯Ό μ œκ³΅ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

JS :

function make () {
  var cl = 1;
  function gett () {
    console.log(cl);
  }
  function sett (val) {
    cl = val;
  }
  return [gett, sett]
}

그리고 μ‹€ν–‰ :

a = make(); g = a[0]; s = a[1];
s(2); g(); // 2
s(3); g(); // 3

파이썬 :

def make ():
  cl = 1
  def gett ():
    print(cl);
  def sett (val):
    cl = val
  return gett, sett

그리고 μ‹€ν–‰ :

g, s = make()
g() #1
s(2); g() #1
s(3); g() #1

이유 : μœ„μ—μ„œ λ§Žμ€ μ‚¬λžŒλ“€μ΄ λ§ν–ˆλ“―μ΄ νŒŒμ΄μ¬μ—μ„œλŠ” λ‚΄λΆ€ λ²”μœ„μ— λ™μΌν•œ μ΄λ¦„μ˜ λ³€μˆ˜μ— λŒ€μž…μ΄ 있으면 λ‚΄λΆ€ λ²”μœ„μ— μƒˆλ‘œμš΄ μ°Έμ‘°κ°€ μž‘μ„±λ©λ‹ˆλ‹€. varν‚€μ›Œλ“œλ‘œ λͺ…μ‹œ 적으둜 μ„ μ–Έν•˜μ§€ μ•ŠλŠ” ν•œ JSμ—μ„œλŠ” κ·Έλ ‡μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ .


이 글은 Python μΉ΄ν…Œκ³ λ¦¬λ‘œ λΆ„λ₯˜λ˜μ—ˆκ³  λ‹˜μ— μ˜ν•΄ 에 μž‘μ„±λμŠ΅λ‹ˆλ‹€.