Syntax | سینتکس
3.02K subscribers
410 photos
108 videos
35 files
377 links
Download Telegram
Syntax | سینتکس
استفاده از الگوی طراحی Singleton در پایتون: class SingletonMeta(type): """ The Singleton class can be implemented in different ways in Python. Some possible methods include: base class, decorator, metaclass. We will use the metaclass because…
یه نکته در خصوص الگوی singleton در پایتون بگم:

استفاده از singleton تو پایتون یکی از bad practice ها به حساب میاد.
بجاش پیشنهاد میشه از قابلیت function cache استفاده کنیم.

مثلا:
@functools.cache
def the_chess_board():
return ChessBoard()
اگه هنوزم میخواید با کلاس single instance رو مدیریت کنید از این روش استفاده کنید:

class ChessBoard:
def __init__(self):
...

@classmethod
@functools.cache
def the_board(cls):
return cls()
لینک مقاله:
https://nedbatchelder.com/blog/202204/singleton_is_a_bad_idea.html

#singleton #python

@Syntax_fa
👍4