๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/Python

[Python] ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ฌธ๋ฒ• ์ •๋ฆฌ

by Danna 2020. 12. 7.
728x90
728x90

Python ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ฌธ๋ฒ•

  • Python์—์„œ ๋ณ€์ˆ˜ _๋Š” ์ตœ๊ทผ ์ถœ๋ ฅ๋œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์žˆ์Œ
  • string์—์„œ \์„ ๋ฌธ์ž๋กœ ํ‘œํ˜„์‹œ \\๊ณผ ๊ฐ™์ด \๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•˜์—ฌ escape์ฒ˜๋ฆฌ
  • ์—ฌ๋Ÿฌ์ค„์˜ string์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด """ ~ """ ํ˜น์€ ''' ~ ''' ์œผ๋กœ ์ž‘์„ฑ
  • pass : ์•„๋ฌด ๋™์ž‘๋„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์Œ
  • unicode string ์‚ฌ์šฉ์‹œ u'~~' ๋ฌธ์ž์—ด ๋‚ด๋ถ€์— ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ์‚ฌ์šฉ์‹œ \nXXXX
    • utf-8๋กœ ์ธ์ฝ”๋”ฉ์‹œ u'~~'.encode('utf-8')
    • ์‹ค์ œ ์ธ์ฝ”๋”ฉ์‹œ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œ์‹œํ•˜๊ธฐ

๋ฌธ๋ฒ• ์—๋Ÿฌ์™€ ์˜ˆ์™ธ

Python์—์„œ ์—๋Ÿฌ๋Š” ํฌ๊ฒŒ SyntaxErrors(๋ฌธ๋ฒ• ์—๋Ÿฌ)์™€ Exceptions(์˜ˆ์™ธ)๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. Exception์˜ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

  • ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ๋“ค์„ except [Error]: ... ๋ธ”๋ฝ์— ์ž‘์„ฑํ•ด์ค€๋‹ค.
  • ์—๋Ÿฌ๋ฅผ ์ง์ ‘ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฒฝ์šฐ raise [ValueError]
try:
    task ...
except [Error]:
    task ...

ํ•จ์ˆ˜ ์ธ์ž์˜ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •

ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ import ๋  ๋•Œ ์ดˆ๊ธฐํ™”๋œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ list, dictionary, class์˜ instance ์ธ ๊ฒฝ์šฐ ์ฐจ์ด์ ์ด ๋ฐœ์ƒํ•œ๋‹ค.

  • f1 ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋งŒ๋“ค ๊ฒฝ์šฐ, List๊ฐ€ ์žฌํ• ๋‹น ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ์— ์ •์˜๋œ L์„ ๊ณ„์† ์‚ฌ์šฉํ•ด ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ๊ฐ’์ด ๋ˆ„์ ๋œ๋‹ค.
  • f2 ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋งค ํ˜ธ์ถœ์‹œ์— ์žฌํ• ๋‹น์„ ํ•ด์ฃผ์–ด์•ผ ์ดˆ๊ธฐ๊ฐ’์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
>>> def f1(a, L=[]):
...     L.append(a)
...     return L
...
>>> print f1(1), f1(2), f1(3)
[1] [1, 2] [1, 2, 3]

>>> def f2(a, L=None):
...     if L is None:
...             L = []
...     L.append(a)
...     return L
...
>>> print f2(1), f2(2), f2(3)
[1] [2] [3]

์ปค๋งจ๋“œ๋ผ์ธ์—์„œ ํ•จ์ˆ˜๋ฅผ defineํ•˜๋Š” ๊ฒฝ์šฐ, def ๋๋‚œ ํ›„, ๋งค๊ฐœ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ class instance ๋ฅผ ์ƒ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, class A์˜ ์ƒ์„ฑ์ž ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
>>> class A():
...     def __init__(self):
...         print("A class init")
...
>>> def f(a=A()):
...     print "f function call"
...
A class init
>>> f()
f function call

ํ•จ์ˆ˜๊ฐ€ import ๋  ๋•Œ ์ดˆ๊ธฐํ™”๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด๋ณด๊ธฐ

  • class A, function f๋ฅผ function_init.py ์ž‘์„ฑํ–ˆ์„ ๊ฒฝ์šฐ
  • module์„ importํ•˜๋ฉด์„œ ํ•จ์ˆ˜๋„ ๊ฐ™์ด import ๋˜์–ด ์ดˆ๊ธฐ๊ฐ’์„ค์ •์ด ๋œ๋‹ค.
# function_init.py
class A():
        def __init__(self):
                print"A Class init"

def f(a=A()):
        print "f function call"
>>> import function_init
A Class init

ํ•จ์ˆ˜์•ˆ์—์„œ ํ•จ์ˆ˜ import

  • ์ค‘๋ณต import์‹œ ๋ฌธ์ œ ์ƒ๊ธฐ๋Š”์ง€?
  • python์€ module์„ ์ค‘๋ณต์œผ๋กœ import ํ•˜๋”๋ผ๋„ ์ค‘๋ณตํ•ด์„œ ๋กœ๋“œํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค.

๊ฐ€๋ณ€์ธ์ž

  • Python์—์„œ๋Š” ์˜ค๋ฒ„๋กœ๋”ฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐœ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ๊ฐ€๋ณ€์ธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜
    *args : tuple ์ž๋ฃŒํ˜• ํ˜•ํƒœ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒฝ์šฐ
    **kwargs : ํ‚ค์›Œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜, dictionary ์ž๋ฃŒํ˜• ํ˜•ํƒœ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒฝ์šฐ
  • *args ์— List๋ฅผ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ์—๋Š” tuple ๋‚ด๋ถ€์— list๊ฐ€ ์ €์žฅ๋œ๋‹ค.
  • ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•œ๋ฒˆ์— ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ *args, **kwargs ์ˆœ์„œ๋กœ ๋ฐ›์•„์•ผํ•œ๋‹ค.
  • **kwargs, *args ์ˆœ์„œ๋กœ ์ •์˜ํ•  ๊ฒฝ์šฐ, (SyntaxError)๋ฌธ๋ฒ•์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
# ๊ฐ€๋ณ€์ธ์ž ์˜ˆ์ œ - arbitrary_argument.py
def func(var, *args, **kwargs):
        print "var type:", type(var), var
        print "args type:", type(args), args
        for arg in args:
                print "- arg:", type(arg), arg
        keys = sorted(kwargs.keys())

        print "kwargs type:", type(kwargs)
        for kw in keys:
                print "- key:value =", kw, ":", kwargs[kw]
from arbitrary_argument import func
>>> func(0, [1,2,3]) # args tuple ๋‚ด๋ถ€์— [1,2,3] list๊ฐ€ ์ €์žฅ๋œ๋‹ค.
var type: <type 'int'> 0
args type: <type 'tuple'> ([1, 2, 3],)
- arg: <type 'list'> [1, 2, 3]

>>> func(0, 1, "arg2", [1,2,3], key1='value1')
var type: <type 'int'> 0
args type: <type 'tuple'> (1, 'arg2', [1, 2, 3])
- arg: <type 'int'> 1
- arg: <type 'str'> arg2
- arg: <type 'list'> [1, 2, 3]
kwargs type: <type 'dict'>
- key:value = key1 : value1

ํ•จ์ˆ˜ ํฌ์ธํ„ฐ

C์—์„œ์˜ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ ์˜ˆ์ œ

executer ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ add ํ•จ์ˆ˜์˜ ๋ฉ”๋ชจ๋ฆฌ์ฃผ์†Œ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. executer ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • add : intํ˜• a, b๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ intํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • executer : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜ํฌ์ธํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
#include <stdio.h>

int add(int a, int b) // intํ˜• a, b๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ intํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
{
    return a + b;
}

void executer(int (*fp)(int, int)) // ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜ํฌ์ธํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
{
    printf("%d\n", fp(10, 20)); // ์ธ์ž๋กœ ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ
}

int main()
{
    executer(add); // executer ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ add์˜ ๋ฉ”๋ชจ๋ฆฌ์ฃผ์†Œ๋ฅผ ์ „๋‹ฌ
    return 0;
}

Python ์—์„œ์˜ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ ์˜ˆ์ œ

Python์—์„œ ํ•จ์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ณ  ์‹ถ์„ ๋•Œ๋Š” ํ•จ์ˆ˜ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜ add๋ฅผ ํ•จ์ˆ˜ executer์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€๋‹ค.

executer๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> def add(a, b):
...     return a + b
...
>>> def executer(func, a, b):
...     print "add function", func(a, b)
...
>>> executer(add, 10, 20)
add function 30

์†์„ฑ๊ฐ’ ํ•จ์ˆ˜ getattr, setattr

getattr(object, name)

  • object์— ์กด์žฌํ•˜๋Š” ์†์„ฑ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • name์— ํ•ด๋‹นํ•˜๋Š” ์†์„ฑ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ AttributeError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค (์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ)

setattr(object, name, value)

  • object์— ์กด์žฌํ•˜๋Š” ์†์„ฑ๊ฐ’์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜, ์ƒˆ๋กœ์šด ์†์„ฑ์„ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ’์„ ๋Œ€์ž…ํ•œ๋‹ค.
# class ์— ๋Œ€ํ•œ ์‚ฌ์šฉ
>>> class C:
...     class_var = 0
...     def __init__(self, instance_var): #์ƒ์„ฑ์ž ๋ถ€๋ถ„
...         self.instance_var = instance_var
...
>>> c1 = C(1) # instance_var=1 ์„ ๊ฐ–๋Š” instance c1๋ฅผ ๋งŒ๋“ ๋‹ค.
>>> getattr(c1, 'instance_var') # c1.x ์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.
1
>>> getattr(c1, 'class_var') # class ์†์„ฑ์ด๋ฏ€๋กœ instance์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜๋‹ค.
0

# instance c1์˜ ์†์„ฑ ์ถ”๊ฐ€
>>> setattr(c1, 'instance_var2', 11) # instance c1์˜ instance_var2 ์†์„ฑ์„ ๋งŒ๋“ค์–ด 11์„ ๋Œ€์ž…ํ•œ๋‹ค.
>>> getattr(c1, 'instance_var2') # instance c1์— instance_var2 ์†์„ฑ์ด ์ƒ๊ธด๋‹ค.
11
>>> getattr(C, 'instance_var2') # class C์—๋Š” instance ์†์„ฑ์ด ์ถ”๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค.
AttributeError : class C has no attribute 'instance_var2'

# class C์˜ ์†์„ฑ ์ถ”๊ฐ€
>>> setattr(C, 'class_var2', 2) # class C์— class_var2๋ผ๋Š” ์†์„ฑ์„ ๋งŒ๋“ค์–ด 2๋ฅผ ๋Œ€์ž…ํ•œ๋‹ค.
>>> print getattr(C, 'class_var2'), getattr(c1, 'class_var2')
2 2 # class ์†์„ฑ์„ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฏ€๋กœ c1์—์„œ๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์†์„ฑ์ด๋‹ค.

# instance c2 ์ƒ์„ฑ
>>> c2 = C(2) # instance_var=1์„ ๊ฐ–๋Š” instance c2๋ฅผ ๋งŒ๋“ ๋‹ค.
>>> print getattr(c2, 'class_var2') # class ์†์„ฑ์ด๋ฏ€๋กœ instance c2์—์„œ๋„ ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜๋‹ค.
3 3
# module์— ๋Œ€ํ•œ ์‚ฌ์šฉ
>>> import simpleClass
>>> getattr(simpleClass, 'C') # module์— ์กด์žฌํ•˜๋Š” class C์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
<class simpleClass.C at 0x7f08ce775600>
>>> getattr(simpleClass.C, 'class_attr') # class C์˜ class_attr์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
[]
>>> setattr(simpleClass, 'D', 0) # module์— ๋ณ€์ˆ˜ D๋ฅผ ๋งŒ๋“ค์–ด 0๋ฅผ ๋Œ€์ž…ํ•œ๋‹ค.
>>> getattr(simpleClass, 'D')
0

 

728x90
728x90