Primrose Docs
  • Home
    • ⛓️Blockchain
      • Avalanche
        • What is AVAX?
      • Ethereum
        • Ethereum Cancun Upgrade Explained(draft)
        • go-ethereum: gas estimate
        • Blockchain Transaction Lifecycle
        • Mempool
        • Gas optimization in Solidity, Ethereum
      • Solidity DeepDive
        • Meta transaction
        • solidity: patterns
        • UUPS vs Transparent
        • Solidity Interface
        • Smart contract storage
        • ERC-2981 Contract
        • Solidity modifier
        • Solidity delete keyword
        • How To Make NFTs with On-Chain Metadata - Hardhat and JavaScript
        • How to Build "Buy Me a Coffee" DeFi dapp
        • How to Develop an NFT Smart Contract (ERC 721) with Alchemy
        • Upgradeable Contract
        • Smart Contract Verification
      • Common
        • Eigenlayer
        • MultiSig(draft)
        • Chain-Based Proof-of- Stake, BFT-Style Proof-of-Stake
        • Byzantine Fault Tolerance
        • Zero-knowledge
        • Hierarchical Deterministic Wallet
        • Maker DAO
        • Defi
        • Uniswap
        • IBC
        • Cosmos
        • Gossip Protocol
        • Tendermint
        • UTXO vs Account
        • Blockchain Layer
        • Consensus Algorithm
        • How does mining work?
        • Immutable Ledger
        • SHA256 Hash
        • Filecoin
        • IPFS - InterPlanetary File System
        • IPFS와 파일코인
        • Livepeer
        • Layer 0
      • Bitcoin
        • BIP for HD Wallet
        • P2WPKH
        • Segwit vs Native Segwit
    • 📖Languages
      • Javascript/Typescript
        • Hoisting
        • This value in Javascript
        • Execution Context
        • About Javscript
        • tsconfig.json
        • Nest js Provider
        • 'return await promise' vs 'return promise'
      • Python
        • Pythonic
        • Python: Iterable, Iterator
        • Uvicorn & Gunicorn
        • WSGI, ASGI
        • Python docstring
        • Decorator in Python
        • Namespace in Python
        • Python Method
      • Go
        • GORM+MySQL Connection Pool
        • Context in golang
        • How to sign Ethereum EIP-1559 transactions using AWS KMS
        • Mongo DB in golang(draft)
        • Golang HTTP Package
        • Panic
        • Golang new/make
        • golang container package
        • errgroup in golang
        • Generic Programming in Golang
        • Goroutine(draft)
    • 📝Database
      • MongoDB in golang
      • Nested loop join, Hash join
      • DB Query plan
      • Index
      • Optimistic Lock Pessimistic Lock
    • 💻Computer Science
      • N+1 query in go
      • Web server 를 구성할 때 Thread, Process 개수를 어떻게 정할 것인가?
      • CAP
      • Socket programming
      • DNS, IP
      • URL, URI
      • TLS과 SSL
      • Caching(draft)
      • Building Microservices: Micro Service 5 Deploy Principle
      • Red Black Tree
      • AOP
      • Distributed Lock
      • VPC
      • Docker
      • All about Session and JWT
      • Closure
      • Singleton Pattern
      • TCP 3 way handshake & 4 way handshake
      • Race Condition
      • Process Address Space 
      • Call by value, Call by reference, Call by assignment
      • Zookeeper, ETCD
      • URL Shortening
      • Raft consensus
      • Sharding, Partitioning
    • 📒ETC
      • K8S SIGTERM
      • SQS
      • Git Branch Strategy: Ship / Show / Ask
      • Kafka
      • Redis Data Types
      • CI/CD
      • How does Google design APIs?
      • Minishell (42 cursus)
      • Coroutine & Subroutine
      • Redis
Powered by GitBook
On this page
  1. Home
  2. Languages
  3. Python

Decorator in Python

Decorator Simple Examples

import time 간단한 문제에서 decorator의 쓰임을 찾아보자.

decorator => 함수를 input으로 받아서 => 새로운 함수를 만들어서 리턴

함수를 받아 새로운 함수를 리턴하는 함수

import time  

def hello(name):
    print("Hello, my name is " + name)


def track_time(func):
    def new_func(*args, **kwargs):
        start_time = time.time()
        func(*args, **kwargs)
        end_time = time.time()
        print(f"Execute Time : {end_time - start_time}")
    return new_func


hello = track_time(hello)
hello("primrose")
# ##
# 확장성을 위해 track_time이라는 데코레이터 함수를 만든다. 인자로 함수를 받고, 내부의 함수를 리턴해준다.
# 리턴되는 함수는 데코레이터 함수 안에서 정의된 함수이고, 확장성을 위해 packing한다.
"""
Hello, my name is primrose
Execute Time : 2.193450927734375e-05
"""

위에서 hello = track_time(hello)라는 식으로 함수를 wrapping했는데,

파이썬에서는 이것을 다음과 같이 표현이 가능하다

다음과 같이 `advanced_hello` 함수 위에 @track_time이라고 적으면 advanced_hello 함수를 인자로 받아서 
track_time 함수가 decorate(장식)하여 새로운 함수가 반환된다.
이름은 advanced_hello 이지만 데코레이터가 있고 없고의 결과는 완전히 다르다.


@track_time
def advanced_hello(name):
    print("Hello, my name is " + name)


advanced_hello("primrose")

"""
Hello, my name is primrose
Execute Time : 3.814697265625e-06
"""

위에서는 데코레이터가 하나였지만, 중첩(nested)도 가능하다.

시간을 측정하는 track_time뿐 아니라, 시작과 끝을 알려주는 데코레이터 함수도 만들었다.

def start(func):
    print("start_func decorator applied")

    def wrapper(*args, **kwargs):
        print("function is called now")
        return func(*args, **kwargs)
    return wrapper


def finish(func):
    print("finish_func decorator applied")

    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        print("function is finished now")
        return result
    return wrapper


@track_time
@finish
@start
def nested_hello(name):
    print("Hello, my name is " + name)


nested_hello("primrose")

"""
start_func decorator applied
finish_func decorator applied
function is called now
Hello, my name is primrose
function is finished now
Execute Time : 5.9604644775390625e-06
"""

아래는 간단한 데코레이터 활용 예시, 피보나치(엄연히 따지자면 메모이제이션)에서 사용 가능 !

def cache_memoization(func):
    __cache = {}

    def wrapper(*args):
        if args in __cache:
            return __cache[args]
        else:
            result = func(*args)
            __cache.update({args: result})
            return result
    return wrapper


@cache_memoization
def fibo(n):
    return n if n < 2 else fibo(n-1) + fibo(n-2)


fibo(100)
PreviousPython docstringNextNamespace in Python

Last updated 1 year ago

📖