๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python/๊ธฐ์ดˆ

Python staticmethod ์™œ ์‚ฌ์šฉํ•ด์•ผํ• ๊นŒ?

by ๐ŸŒปโ™š 2021. 10. 4.

Python staticmethod

Python staticmethod๋Š” @staticmethod ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ ์ •์  ๋ฉ”์†Œ๋“œ ์ž…๋‹ˆ๋‹ค.

  • ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ self ์ธ์ž๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.
  • ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ง์ ‘ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ธ์Šคํ„ด์Šค ์†์„ฑ์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค.

์ด๋Ÿฐ ํŠน์ง•๋“ค์ด ์žˆ์–ด์„œ ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ๋ฉ”์†Œ๋“œ๋“ค์€ ์ฃผ๋กœ ๋”ฐ๋กœ utils๋ผ๋Š” ๋ชจ๋“ˆ๋กœ ๋นผ์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ... ํด๋ž˜์Šค ๋‚ด์—์„œ๋Š” ์™œ  staticmethod๊ฐ€ ํ•„์š”ํ• ๊นŒ์š”? ์•„๋ž˜ ์‹ค์Šต์„ ํ†ตํ•ด์„œ ์ข€ ๋” ์ž์„ธํžˆ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

Playlist ์‹ค์Šต

import random
import json
import hashlib


class Playlist:
    class NoMusicInPlayListException(Exception):
        """Raised when music is not in the playlist."""
        pass

    def __init__(self):
        self._playlist = []
        self.current_play = None

    def show(self):
        print(self._playlist)

    def add(self, music):
        self._playlist.append(music)

    def remove(self, title):
        for music in self._playlist:
            if music['title'] == title:
                self._playlist.remove(music)
                break

    def play(self, title):
        for music in self._playlist:
            if title == music['title']:
                self.current_play = music
                print(f'"{self.current_play["title"]}" is playing...')
                break
        else:
            raise self.NoMusicInPlayListException(f'"{music}" is not in the playlist.')

    def random_play(self):
        self.current_play = random.choice(self._playlist)
        print(f'"{self.current_play["title"]}" is playing...')

    @staticmethod
    def playtime_diff(music1, music2):
        return abs(music1['play_time'] - music2['play_time'])

    @staticmethod
    def hash(music):
        music_str = json.dumps(music, sort_keys=True).encode()
        return hashlib.sha256(music_str).hexdigest()


if __name__ == "__main__":
    p = Playlist()

    p.add({"title": "Circle", "artist": "Post Malone", "play_time": 3.47})
    p.add({"title": "Better Now", "artist": "Post Malone", "play_time": 3.53})

    p.show()
    p.random_play()

    print(p.hash({"title": "Better Now", "artist": "Post Malone"}))
    print(
        p.playtime_diff(
            {"title": "Circle", "artist": "Post Malone", "play_time": 3.47},
            {"title": "Better Now", "artist": "Post Malone", "play_time": 3.53}
        )
    )

 ์œ„ ์ฝ”๋“œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ Playlist๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋…ธ๋ž˜๋ฅผ ์ถ”๊ฐ€, ์‚ญ์ œํ•˜๊ณ  ์žฌ์ƒํ•˜๋Š” ํ˜•์‹์˜ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. staticmethod๋กœ playtime_diff์™€ hash ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 ๋‘ ํ•จ์ˆ˜๋“ค์˜ ๊ณตํ†ต์ ์€ ํด๋ž˜์Šค์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์Œ... ์–ด์จ‹๋“  music์ด๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋“ค๊ณ  ๊ฐ€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์™„์ ผํžˆ ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ ๋Š” ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ... ํด๋ž˜์Šค ๋‚ด์˜ ๋‹ค๋ฅธ ์†์„ฑ์ด๋‚˜ ํ•จ์ˆ˜์— ์˜์กดํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ํ•จ์ˆ˜๋กœ staticํ•˜๊ฒŒ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ๋งž๋‹ค๊ณ  ํŒ๋‹จ ๋ฉ๋‹ˆ๋‹ค.

 

 ์ œ๊ฐ€ ๋‹ค๋ฅธ Python ์ฝ”๋“œ๋“ค์„ ๋ณด๋ฉด์„œ ๋งค๋ฒˆ "์™œ ์ด ํ•จ์ˆ˜๋ฅผ staticmethod๋กœ ์ •์˜ํ–ˆ๋Š”์ง€..." ์˜๋ฌธ์„ ๊ฐ€์กŒ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋Œ€๋ถ€๋ถ„ ๊ฐ™์€ ํŒจํ„ดํ•˜๋‚˜๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. self๋ฅผ ์ด์šฉํ•ด์„œ ํด๋ž˜์Šค์˜ ์†์„ฑ์ด๋‚˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ... ์ฆ‰, ์˜์กด์„ฑ์ด ์—†๋Š” ๊ฒฝ์šฐ ๋ชจ๋‘ staticmethod๋กœ ์ •์˜ํ–ˆ์Šค๋นˆ๋‹ค.

 

staticmethod ์žฅ์ 

static method๋Š” ๋‹จ์ˆœํžˆ ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ํ•จ์ˆ˜์— ์‚ฌ์šฉํ•œ๋‹ค๋ผ๊ณ  ์ดํ•ดํ•˜๊ธฐ ๋ณด๋‹ค๋Š”... ์•„๋ž˜ 4๊ฐ€์ง€ ์žฅ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

  • self ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ์ธ์Šคํ„ด์Šคํ™”ํ•  ๋•Œ static method์— ๋Œ€ํ•ด์„œ๋Š” bound-method๋ฅผ ์ƒ์„ฑํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์–ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ์ธ์Šคํ„ด์Šค ์˜ค๋ธŒ์ ํŠธ ์ž์ฒด์— ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜์—ฌ ์ฝ”๋“œ์˜ ๊ฐ€์‹œ์„ฑ์ด ์ข‹์•„์ง„๋‹ค.

 

๋Œ“๊ธ€