Advanced Python - lambda [4/7]

Seobยท2020๋…„ 7์›” 23์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
20/36

Lambda? ๐Ÿคท๐Ÿปโ€โ™‚๏ธ

๋žŒ๋‹ค๋Š” ์ธ๋ผ์ธ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ Anonymous functions(์ต๋ช… ํ•จ์ˆ˜) ๋˜๋Š” Lambda expression(๋žŒ๋‹ค ํ‘œํ˜„์‹)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ํ•จ์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉ ํ•˜์ง€ ์•Š์œผ๋ ค๋Š” ๊ฒฝ์šฐ lambda ํ•จ์ˆ˜๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜ํ•จ์ˆ˜์™€ ํ˜•ํƒœ๋„ ๋น„์Šทํ•˜๊ณ  ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

์ผ๋ฐ˜ ํ•จ์ˆ˜๋Š” defํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ•์— ๋”ฐ๋ฅธ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์ด๊ณ , ํ•จ์ˆ˜ ๋˜ํ•œ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค์ด๋‹ค.

์ฝ”๋“œ์™€ ์ด๋ฆ„์„ ๋‹ด์•„์„œ ํ•จ์ˆ˜ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ ๊ฐ์ฒด๋ฅผ ํ•จ์ˆ˜์ด๋ฆ„๊ณผ ๋™์ผํ•œ ๋ณ€์ˆ˜์— ๋‹ด๋Š” ๊ณผ์ •์„ def ํ‚ค์›Œ๋“œ๊ฐ€ ๋Œ€์‹ ํ•œ๋‹ค.

ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์€ ์‹œ์ ์—์„œ ํ•จ์ˆ˜๊ฐ์ฒด๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์„œ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ํ˜ธ์ถœ๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ๋œ๋‹ค. ๋งŒ์•ฝ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉ๋  ํ•จ์ˆ˜๋ผ๋ฉด ๋ถˆํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋‚ญ๋น„๋œ๋‹ค.

๋žŒ๋‹คํ•จ์ˆ˜๋Š” lambdaํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

๋žŒ๋‹คํ•จ์ˆ˜๋Š” ๊ฒฐ๊ณผ๋ฅผ return ํ‚ค์›Œ๋“œ ์—†์ด ์ž๋™์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ต๋ช…ํ•จ์ˆ˜๋ผ๋Š” ์ด๋ฆ„์ฒ˜๋Ÿผ ํ•จ์ˆ˜์— ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ๋งŒ๋“ ๋‹ค.

lambda x : x + 1

์œ„์˜ ๋žŒ๋‹ค ํ•จ์ˆ˜๋Š” x์ธ argument์— ์ˆซ์ž๋ฅผ ๋ฐ›์•„์„œ 1์„ ๋”ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋žŒ๋‹ค๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜์™€ ๋™์‹œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

(lambda x : x + 1)(3)
>>>
4

ํ•จ์ˆ˜์— ์ด๋ฆ„์ด ์—†๊ณ  ์ €์žฅ๋œ ๋ณ€์ˆ˜๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ, ์ •์˜์™€ ๋™์‹œ์— ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜๋Š” ์žˆ๋‹ค.

lambda_func = lambda x, y, z : x + y + z
lambda_func(1, 2, 3)
>>>
6

assignment

  1. ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๊ณ  print๋ฌธ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ  types ๋ชจ๋“ˆ์— LambdaType ์™ธ์—๋„ ์–ด๋–ค ํƒ€์ž…๋“ค์ด ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•ด ๋ณด์„ธ์š”.
import types

f = lambda x,y,z : x+y+z

print(f)
print(type(f))
print( type(f) == types.LambdaType)
>>>
<function f at 0x7fbd339684c0>
<class 'function'>
True

Standard Interpreter Types

์ด ๋ชจ๋“ˆ์€ ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ์ด๋ฆ„์„ ์ œ๊ณตํ•œ๋‹ค. ์ด ์ด๋ฆ„๋“ค์€ ๋ณดํ†ต isinstance(),๋‚˜ issubclass() ์ฒดํฌ์— ์‚ฌ์šฉ๋œ๋‹ค.

  • types.FunctionType
  • types.LambdaType
  • types.GeneratorType
  • types.CoroutineType
  • types.AsyncgeneratorType
  • types.CellType
  • types.MethodType
  • types.BuiltinFunctionType
  • types.BuiltinMethodType
    ์ด ์™ธ์—๋„ ๋งŽ์€ ํƒ€์ž… ์ด๋ฆ„์ด ์žˆ๋‹ค.
    https://docs.python.org/3/library/types.html
  1. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋น„๋ฐ€๋ฒˆํ˜ธ์˜ ๊ธธ์ด์™€ ๋Œ€๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ๊ฒƒ์„ ํ™•์ธํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
def check_password(password):
    if len(password) < 8:
        return 'SHORT_PASSWORD'

    if not any(c.isupper() for c in password):
        return 'NO_CAPITAL_LETTER_PASSWORD'

    return True

์ดํ•จ์ˆ˜์— ์žˆ๋Š” if๋ฌธ ๋‘๊ฐœ๋ฅผ ๋žŒ๋‹คํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

์•„๋ž˜์˜ lambdas ๋ฆฌ์ŠคํŠธ์•ˆ์— ๋‘๊ฐœ์˜ ๋žŒ๋‹คํ‘œํ˜„์‹์„ ์ž‘์„ฑํ•ด์•ผํ•˜๋ฉฐ ์ฃผ์„์œผ๋กœ ํ‘œ์‹œ๋œ ํ”„๋ฆฐํŠธ๊ฐ€ ์ถœ๋ ฅ๊ฒฐ๊ณผ๋กœ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

lambdas = [ 

]

def check_password_using_lambda(password):

    for f in lambdas:
        if f(password) is not None:
            return f(password)

    return True


print( check_password_using_lambda('123') )            # SHORT_PASSWORD
print( check_password_using_lambda('12356789f') )      # NO_CAPITAL_LETTER_PASSWORD
print( check_password_using_lambda('123456789fF') )    # True

lambda์˜ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

lambda <arguments> : <Return Value if condition is True> if <condition> else <Return Value if condition is False>

์ด ๋ฌธ๋ฒ•์„ ๋ณด๊ณ  ์ ์šฉ์‹œ์ผœ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

lambdas = [
    lambda password: 'SHORT_PASSWORD' if len(password) < 8 else None,
    lambda password: 'NO_CAPITAL_LETTER_PASSWORD' if not any(
        c.isupper() for c in password) else None
]


def check_password_using_lambda(password):
    for f in lambdas:
        if f(password) is not None:
            return f(password)
    return True


print(check_password_using_lambda('123'))            # SHORT_PASSWORD
# NO_CAPITAL_LETTER_PASSWORD
print(check_password_using_lambda('12356789f'))
print(check_password_using_lambda('123456789fF'))    # True

>>>

SHORT_PASSWORD
NO_CAPITAL_LETTER_PASSWORD
True
profile
Hello, world!

0๊ฐœ์˜ ๋Œ“๊ธ€