lambda : ๋ฐํ์์์ ์์ฑํด์ ์ฌ์ฉํ ์ ์๋ ์ต๋ช
ํจ์
lambda ์ธ์ : ํํ์
l = list(range(1,11))
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = list(map(lambda n:n*n, l))
print(m)
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
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 = [
]
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
์๋์ ๊ฐ์ด ์์ ํ๋ฉด ์คํ๋๋ค.
lambdas = [lambda x : 'SHORT_PASSWORD' if len(x) < 8 else None, lambda x : 'NO_CAPITAL_LETTER_PASSWORD' if not any(c.isupper() for c in x)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 print( check_password_using_lambda('12356789f') ) # NO_CAPITAL_LETTER_PASSWORD print( check_password_using_lambda('123456789fF') ) # True
types.FunctionType
types.LambdaType
types.GeneratorType
types.CoroutineType
types.AsyncGeneratorType
class types.CodeType(**kwargs)
types.CellType
types.MethodType
types.BuiltinFunctionType
types.BuiltinMethodType
types.WrapperDescriptorType
types.MethodWrapperType
types.MethodDescriptorType
types.ClassMethodDescriptorType
class types.ModuleType(name, doc=None)
class types.TracebackType
types.FrameType
types.GetSetDescriptorType
types.MemberDescriptorType
class types.MappingProxyType(mapping)
์ถ์ฒ : https://docs.python.org/3.8/library/types.html
==== ์์ ์ค ===
import time
def ์ฃผ๋ฌธ๋ฐ๊ธฐ():
for i in range(5):
print(f'์ฃผ๋ฌธ๋ฐ๊ธฐ {i}')
time.sleep(1)
def ์ฐํธ๋ฐ์ก():
for i in range(5):
print(f'์ฐํธ๋ฐ๊ธฐ {i}')
time.sleep(0.5)
์ฃผ๋ฌธ๋ฐ๊ธฐ()
์ฐํธ๋ฐ์ก()
์ฃผ๋ฌธ ๋ฐ๊ธฐ๊ฐ ๋๋์ผ ์ฐํธ๋ฐ์ก์ด ๋์ด
์ด๊ฑด ์ฐ๋ ๋๋ก ๊ตฌ์ฑํจ . ๋ฉ์ธ ์ฐ๋ ๋ ํ๋๋ก ๊ตฌ์ฑ
๋ง์ฝ ์ฃผ๋ฌธ๋ฐ๊ธฐ์ ์ฐํธ๋ฐ์ก ๋๊ธฐ์ ํ๊ณ ์ถ๋ค > ์ค๋ ๋๋ฅผ ๋๋ ค์ฃผ๋ฉด ๋จ
import time
import threading
def ์ฃผ๋ฌธ๋ฐ๊ธฐ():
for i in range(5):
print(f'์ฃผ๋ฌธ๋ฐ๊ธฐ {i}')
time.sleep(1)
def ์ฐํธ๋ฐ์ก():
for i in range(5):
print(f'์ฐํธ๋ฐ๊ธฐ {i}')
time.sleep(0.5)
th1 = threading.Thread(target=์ฃผ๋ฌธ๋ฐ๊ธฐ)
th2 = theeading.Thread(target = ์ฐํธ๋ฐ์ก)
th1.start()
th2.start()
์ฐ๋ ๋๋ ๋ฌด์กฐ๊ฑด ๋ง์๊ฒ ์ข์ ๊ฑด ์๋.
์คํ๋ ค ํจ์จ์ด ๋ ๋จ์ด์ง ์ ๋ ์์
ํฐ๋ฏธ๋ ์ข
๋ฃํด๋ ์ฐ๋ ๋๋ ์ด์ ์์
๋ฐ๋ชฌ ์ฐ๋ ๋๋ฅผ ์ฃผ๋ฉด > ๋ฉ์ธ ์ฐ๋ ๋๊ฐ ์ฃฝ์ผ๋ฉด ์ฐ๋ ๋๊ฐ ์ข
๋ฃ๊ฐ ์๋ผ๋ ์ข
๋ฃ๋จ.
๋ฉ์ธ ์ฐ๋ ๋๋ ๋์ ๋ณด์ด์ง ์์ง๋ง ์ฐ๋ ๋๋ฅผ ์์ํ ํ๋ก๊ทธ๋จ์ด ๋ฉ์ธ ์ฐ๋ ๋๊ฐ ์๋ ๊ฒ
ํ์ด์ฌ์์ ์ฐ๋ ๋๋ gil