# Python 03 Function

hsngjuΒ·2020λ 10μ 1μΌ
0

## python

3/6

ν·κ°λ¦¬λ κ²κ³Ό λμ€μ λ€μ ν λ² νμΈν΄μΌ ν  κ°λμ μ  μ£Όκ΄μ λ°λΌ μ ννκ³  κΈ°λ‘ν κΈμ΄κΈ° λλ¬Έμ νμ΄μ¬μμ νμ©λλ κ°λμ΄ λ€ ν¬ν¨λκ² μλλΌλ κ²! μμμ£ΌμΈμ..

• κ°λμ±
• μ μ§λ³΄μ

## βΎ ν¨μμ μλ ₯

def func(x):
return x + 2
• λ§€κ°λ³μ
• x λ λ§€κ°λ³μ(parameter)
• μλ ₯μ λ°μ ν¨μ λ΄λΆμμ νμ©ν  λ³μλΌκ³  μκ°νλ©΄ λλ€.
• ν¨μμ μ μ λΆλΆμμ λ³Ό μ μλ€.
func(2)
• μ λ¬μΈμ
• 2 λ (μ λ¬)μΈμ(argument)
• μ€μ λ‘ μ λ¬λλ μλ ₯κ°μ΄λΌκ³  μκ°νλ©΄ λλ€.
• ν¨μλ₯Ό νΈμΆνλ λΆλΆμμ λ³Ό μ μλ€.

## βΎ ν¨μμ μΈμ

ν¨μλ μλ ₯κ°(input)μΌλ‘ μΈμ(argument)λ₯Ό λκ²¨μ€ μ μμ΅λλ€.

### μμΉμΈμ

• ν¨μλ κΈ°λ³Έμ μΌλ‘ μΈμλ₯Ό μμΉλ‘ νλ¨ν©λλ€.
def cylinder(r, h):
return r**2 * 3.14 * h
cylinder(5,2)

### κΈ°λ³ΈμΈμκ°

ν¨μκ° νΈμΆλ  λ, μΈμλ₯Ό μ§μ νμ§ μμλ κΈ°λ³Έ κ°μ μ€μ ν  μ μμ΅λλ€. == λν΄νΈ

def greeting(name='μ΅λͺ'):
return f'{name}, μλ?'

greeting()
• λ¨, κΈ°λ³Έ μΈμκ°(Default Argument Value)μ κ°μ§λ μΈμ λ€μμ κΈ°λ³Έ κ°μ΄ μλ μΈμλ₯Ό μ¬μ©ν  μλ μμ΅λλ€.

# μ€λ₯
def greeting(name='μ΅λͺ',age):
...
# μμ 
def greeting(age,name='μ΅λͺ'):
...

### ν€μλμΈμ

ν€μλ μΈμλ μ§μ  λ³μμ μ΄λ¦μΌλ‘ νΉμ  μΈμλ₯Ό μ λ¬ν  μ μμ΅λλ€.

def greeting(age, name='john'):
return f'{name}μ {age}μ΄μλλ€.'

greeting(name='μ² μ', age=24)
greeting(24, name='μ² μ')
• λ¨ μλμ κ°μ΄ ν€μλμΈμλ₯Ό νμ©ν μ΄νμ μμΉμΈμλ₯Ό μ¬μ©ν  μ μμ΅λλ€.

#μ€λ₯
greeting(age=24, 'μ² μ')

### κ°λ³μΈμλ¦¬μ€νΈ

κ°μκ° μ ν΄μ§μ§ μμ μμμ μΈμλ₯Ό λ°κΈ° μν΄μλ κ°λ³ μΈμ λ¦¬μ€νΈ*argsλ₯Ό νμ©ν©λλ€.

κ°λ³ μΈμ λ¦¬μ€νΈλ tuple ννλ‘ μ²λ¦¬κ° λλ©°, λ§€κ°λ³μμ *λ‘ ννν©λλ€.

# λ³΄ν΅, μ΄ κ°λ³ μΈμ λ¦¬μ€νΈλ λ§€κ°λ³μ λͺ©λ‘μ λ§μ§λ§μ μ΅λλ€.
def func(a, b, *args):

### κ°λ³(μμ) ν€μλ μΈμ

μ ν΄μ§μ§ μμ ν€μλ μΈμλ€μ dict ννλ‘ μ²λ¦¬κ° λλ©°, **λ‘ ννν©λλ€.

λ³΄ν΅ kwagrsλΌλ μ΄λ¦μ μ¬μ©νλ©°, **kwargsλ₯Ό ν΅ν΄ μΈμλ₯Ό λ°μ μ²λ¦¬ν  μ μμ΅λλ€.

def func(**kwargs):

## βΎ ν¨μμ μ€μ½ν

ν¨μλ μ½λ λ΄λΆμ κ³΅κ°(scope)λ₯Ό μμ±ν©λλ€. ν¨μλ‘ μμ±λ κ³΅κ°μ μ§μ­μ€ν¬ν¬(local scope)λΌκ³  λΆλ¦¬λ©°, κ·Έ μΈμ κ³΅κ°μΈ μ μ­ μ€ν¬ν¬(global scope)μ κ΅¬λΆλ©λλ€.

• μ μ­ μ€μ½ν (global) : μ½λ μ΄λμμλ  μ°Έμ‘°ν  μ μλ κ³΅κ°
• μ μ­ λ³μ(global variable): μ μ­ μ€μ½νμ μ μλ λ³μ
• μ§μ­ μ€μ½ν(local) : ν¨μκ°λ§λ  μ€μ½νλ‘ ν¨μ λ΄λΆμμλ§ μ°Έμ‘°ν  μ μλ κ³΅κ°
• μ§μ­ λ³μ(local variable): λ‘μ»¬ μ€μ½νμ μ μλ λ³μ

### μ΄λ¦ κ²μ κ·μΉ

νμ΄μ¬μμ μ¬μ©λλ μ΄λ¦(μλ³μ)λ€μ μ΄λ¦κ³΅κ°(namespace)μ μ μ₯λμ΄ μμ΅λλ€.

μ΄κ²μ, LEGB Rule μ΄λΌκ³  λΆλ₯΄λ©°, μλμ κ°μ μμλ‘ μ΄λ¦μ μ°Ύμλκ°λλ€.

• Local scope: μ μλ ν¨μ

• Enclosed scope: μμ ν¨μ

• Global scope: ν¨μ λ°μ λ³μ νΉμ importλ λͺ¨λ

• Built-in scope: νμ΄μ¬μμ λ΄μ₯λμ΄ μλ ν¨μ λλ μμ±

### λ³μμ μλͺμ£ΌκΈ°(lifecycle)

λ³μμ μ΄λ¦μ κ°μμ μλͺμ£ΌκΈ°(lifecycle)κ° μμ΅λλ€.

• λΉνΈμΈ μ€μ½ν(built-in scope): νμ΄μ¬μ΄ μ€νλ μ΄νλΆν° μμν μ μ§

• μ μ­ μ€μ½ν(global scope): λͺ¨λμ΄ νΈμΆλ μμ  μ΄ν νΉμ μ΄λ¦ μ μΈλ μ΄νλΆν° μΈν°νλ¦¬ν°κ° λλ λ κΉμ§ μ μ§

• μ§μ­(ν¨μ) μ€μ½ν(local scope): ν¨μκ° νΈμΆλ  λ μμ±λκ³ , ν¨μκ° κ° μ’λ£λ  λκΉμ§ μ μ§ (ν¨μ λ΄μμ μ²λ¦¬λμ§ μλ μμΈλ₯Ό μΌμΌν¬ λ μ­μ λ¨)

κΈ°λ‘νλ μ΅κ΄μ μ½κ² λ¬΄λμ§μ§ μμμ.