✌️
Studylog
See More
Studylog
Studylog
  • INTRO
  • AWS
    • AWS101
      • Virtualization and the AWS structures
      • AWS account and free-tier
      • AWS IAM
      • AWS EC2
        • EC2 basic
        • ENI and EIP
        • Elastic Block Storage
        • Auto Scaling
        • Elastic Load Balancer
  • IaC
    • Terraform
      • License Change
      • Tutorial
      • Module
      • Versioning
  • Airflow
    • Airflow-Ninja
      • Introduction and Goal
      • Tutorial: Settings
      • Tutorial: Module Import, Alert
  • Docker
    • Production with Docker
      • Customizing root directory
  • Network
    • Network-Handbook
      • Introduction and Goal
      • OSI7Layer
      • DNS
      • SSL/TLS
  • Comupter Architecture
    • Basic
      • Introduction and Goal
      • Chapter 1. The Internal Language of Computers
      • Chapter 2. Combinatorial Logic
      • Chapter 3. The Essentials of Memory and Disk Sequential Logic
    • Hands-on
      • Introduction and Goal
      • theory
        • Chapter 1. Logic Gates
        • Chapter 2. ALU
      • project
        • Project 1. Elementary logic gates implement
        • Project 2. Boolean Arithmetic
  • Java
    • Readable Code
      • ν•™μŠ΅ λͺ©μ 
      • 좔상화
      • 논리적 사고 흐름
      • 객체지ν–₯ 이둠
      • 객체지ν–₯ μ½”λ“œ μ μš©ν•˜κΈ°
      • μ½”λ“œ 닀듬기
      • 읽기 쒋은 μ½”λ“œλ₯Ό 도와쀄 μ‘°μ–Έλ“€
  • Spring Boot
    • Practical Testing
      • ν…ŒμŠ€νŠΈ 사전 지식
      • μŠ€ν”„λ§ λ ˆμ΄μ–΄λ“œ μ•„ν‚€ν…μ²˜ ν…ŒμŠ€νŠΈν•˜κΈ°
        • Persistence Layer
        • Business Layer
    • μŠ€ν”„λ§ 핡심 원리 - 기본편
      • 객체 μ§€ν–₯ 섀계와 μŠ€ν”„λ§
      • μŠ€ν”„λ§ 핡심 원리 이해
        • 예제 λ§Œλ“€κΈ°
        • 객체 μ§€ν–₯ 원리 적용
        • μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆμ™€ μŠ€ν”„λ§ 빈
  • Python
    • Effective Python
      • Introduction and Goal
      • Closure: Lazy Evaluation And Eager Evaluation
      • Python public attributes are better getter-setter
      • No refactoring attribute, we can use property decorator
      • You can do it, but it means you don't have to
  • Playgrounds
    • Java Playground
      • ν•™μŠ΅ λͺ©μ 
      • 숫자 야ꡬ κ²Œμž„
        • ν•™μŠ΅ ν…ŒμŠ€νŠΈ
        • λ¬Έμžμ—΄ 계산기
        • 숫자 야ꡬ κ²Œμž„ κ΅¬ν˜„
        • 숫자 야ꡬ κ²Œμž„ λ‹€μ‹œ κ΅¬ν˜„ν•˜κΈ°
      • μžλ™μ°¨ κ²½μ£Ό
        • λ¬Έμžμ—΄ λ§μ…ˆ 계산기
        • μžλ™μ°¨ κ²½μ£Ό λ―Έμ…˜ κ΅¬ν˜„
      • μ’Œν‘œ 계산기
        • μ—°λ£Œ μ£Όμž…
        • μ’Œν‘œ 계산기 λ―Έμ…˜ κ΅¬ν˜„
    • Infra Playground
      • VPC: 망뢄리 그리고 ν…ŒμŠ€νŠΈ
      • μ»¨ν…Œμ΄λ„ˆ 사전 지식
      • ν™”λ©΄ μ„±λŠ₯ κ°œμ„  μ „ ν•™μŠ΅ ν…ŒμŠ€νŠΈ
      • SSM: Session Manager
      • SQL, 이 μ •λ„λŠ” μ•Œμ•„μ•Όμ§€ 😎
      • Subway-Map
        • 망 κ΅¬μ„±ν•˜κΈ°
        • μ„œλ²„ κ΅¬μ„±ν•˜κΈ°
        • ν™”λ©΄ μ„±λŠ₯ κ°œμ„ ν•˜κΈ°
      • Conference Platform
        • 망 κ΅¬μ„±ν•˜κΈ°
        • μ„œλ²„ κ΅¬μ„±ν•˜κΈ°
        • ν™”λ©΄ μ„±λŠ₯ κ°œμ„ ν•˜κΈ°
  • Tools
    • SOPS
    • Bruno
    • πŸ–₯️FCK-NAT
    • 🧊Pulumi
Powered by GitBook
On this page
  1. Python
  2. Effective Python

Closure: Lazy Evaluation And Eager Evaluation

PreviousIntroduction and GoalNextPython public attributes are better getter-setter

Last updated 10 months ago

Properties

2024.08.09

CHAPTER 2. 동등성과 ν˜Όλ™μ„±

Lazy Evaluation

Closure Lazy Evaluation And Eager Evaluation

  • ν•¨μˆ˜ 내뢀에 μ§€μ—­ 객체λ₯Ό μƒμ„±ν•˜κ³  폐쇄적인 λ²”μœ„λ₯Ό μ‚¬μš© ν•˜λŠ” 일급 ν•¨μˆ˜λ₯Ό λ‹€λ£° λ•Œ μ§€μ—° 연산을 μžŠμ§€ λ§μ•„μ•Όν•œλ‹€.

    def make_adders(addends):
        funcs = []
        for addend in addends:
            print(f"for iter addend: {addend}")
            funcs.append(lambda x: print(f"x: {x}, addend: {addend}") or x + addend)
        return funcs
    
    if __name__ == "__main__":
        adders = make_adders([10, 100, 1000])
        for adder in adders:
            print(f"adder: {adder}")
            print(adder(5))

    Lazy Evaluation에 μ˜ν•˜μ—¬ addend λŠ” λ°˜λ³΅λ¬Έμ— κ°€μž₯ λ§ˆμ§€λ§‰ 값인 1000으둜 μ‚°μ • λœλ‹€. κ·Έ μ΄μœ λŠ” ν΄λ‘œμ €κ°€ 폐쇄적인 λ²”μœ„λ₯Ό μ‚¬μš© ν•˜λŠ” 일급 ν•¨μˆ˜λΌλŠ” 것에 μžˆλ‹€.

  • λ™μž‘ μˆœμ„œ

  1. λ°˜λ³΅λ¬Έμ„ 순회 ν•˜λ©° funcs λΌλŠ” List에 ν•¨μˆ˜μ˜ 였브젝트 μ£Όμ†Œλ₯Ό λ‹΄μ•„λ‘”λ‹€.

  2. 반볡문 μˆœνšŒκ°€ λλ‚˜λ©΄ ν•¨μˆ˜μ˜ μ£Όμ†Œκ°’μ΄ λ‹΄κ²¨μžˆλŠ” Listλ₯Ό λ°˜ν™˜ν•œλ‹€.

  3. adders λ₯Ό λ‹€μ‹œ 순회 ν•˜λ©΄μ„œ 내뢀에 μ‘΄μž¬ν•˜λŠ” ν•¨μˆ˜ 였브젝트 μ£Όμ†Œ 값을 이용 ν•˜μ—¬ Callable 객체λ₯Ό μ‹€ν–‰ μ‹œν‚¨λ‹€.

    • 였브젝트 μ£Όμ†Œκ°€ 담겨져 있으며 μ΅œμ’…μ μœΌλ‘œ λ©”λͺ¨λ¦¬μ— 남아 μžˆλŠ” addend 값인 1000을 이용 ν•˜κ³  x에 νŒŒλΌλ―Έν„° 정보인 5λ₯Ό μ‚¬μš© ν•˜κ²Œ λœλ‹€.

  • κ²°κ³Ό

    for iter addend: 10
    for iter addend: 100
    for iter addend: 1000
    adder: <function make_adders.<locals>.<lambda> at 0x7fd5d929e200>
    x: 5, addend: 1000
    1005
    adder: <function make_adders.<locals>.<lambda> at 0x7fd5d929e290>
    x: 5, addend: 1000
    1005
    adder: <function make_adders.<locals>.<lambda> at 0x7fd5d929e320>
    x: 5, addend: 1000
    1005
    x: 5, addend: 1000

Eager Evaluation

  • Lazy Evaluation 방식을 κ°•μ œλ‘œ Eager Evaluation으둜 λ³€κ²½ ν•  수 μžˆλ‹€. λ©”λͺ¨λ¦¬μ˜ λ§ˆμ§€λ§‰ 값을 μ‚¬μš© ν–ˆλ˜ 방식을 λΉ—λŒ€μ–΄ 보면 default 값을 μ§€μ • ν•΄μ£Όμ—ˆμ„ λ•Œ 상쇄 μ‹œν‚¬ 수 μžˆμ„ 것이닀.

  • κΈ°μ–΅ ν•΄λ³΄μžλ©΄ νŒŒμ΄μ¬μ€ μ–Έμ œλ‚˜ 객체λ₯Ό μ°Έμ‘°ν•˜λŠ” 값을 μ‚¬μš©ν•˜μ—¬ μž¬μ‚¬μš©λ₯ μ„ λ†’νžŒλ‹€.

    def make_adders(addends):
        funcs = []
        for addend in addends:
            print(f"for iter addend: {addend}")
            funcs.append(lambda x, _addend=addend: print(f"x: {x}, addend: {_addend}") or x + _addend)
        return funcs
    
    
    if __name__ == "__main__":
        adders = make_adders([10, 100, 1000])
        for adder in adders:
            print(f"adder: {adder}")
            print(adder(5))
  • 달라진 점

    • μ—¬κΈ°μ„œ μ€‘μš”ν•œ ν¬μΈνŠΈλŠ” ν•¨μˆ˜ νŒŒλΌλ―Έν„°μ˜ κΈ°λ³Έ 값을 μ‚¬μš© ν–ˆλ‹€λŠ” 점이닀. _addend=addend λŠ” ν˜„μž¬ 값을 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ‚¬μš© ν•  수 μžˆλ„λ‘ 객체 참쑰값을 λ„£μ–΄μ£Όμ—ˆλ‹€.

    • Lambda λ‚΄λΆ€μ—μ„œ Print ν•  수 μžˆλ‹€λŠ” 것에 또 ν•œλ²ˆ λ†€λΌμš΄ μ‚¬μ‹€μž„μ„ μ•Œ 수 μžˆλ‹€.

  • κ²°κ³Ό

    addend: 10
    addend: 100
    addend: 1000
    adder: <function make_adders.<locals>.<lambda> at 0x7f76727ee200>
    x: 5, addend: 10
    15
    adder: <function make_adders.<locals>.<lambda> at 0x7f76727ee290>
    x: 5, addend: 100
    105
    adder: <function make_adders.<locals>.<lambda> at 0x7f76727ee320>
    x: 5, addend: 1000
    1005
πŸ“
πŸ“„
πŸ“Ž
λ˜‘λ˜‘ν•˜κ²Œ μ½”λ”©ν•˜λŠ”λ²• μ½”λ”©μ˜ 기술 51