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 استفاده کنیم.
مثلا:
https://nedbatchelder.com/blog/202204/singleton_is_a_bad_idea.html
#singleton #python
@Syntax_fa
استفاده از singleton تو پایتون یکی از bad practice ها به حساب میاد.
بجاش پیشنهاد میشه از قابلیت function cache استفاده کنیم.
مثلا:
@functools.cacheاگه هنوزم میخواید با کلاس single instance رو مدیریت کنید از این روش استفاده کنید:
def the_chess_board():
return ChessBoard()
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
Nedbatchelder
Singleton is a bad idea
Design patterns are a great way to think about interactions among classes. But the classic Singleton pattern is bad: you shouldn’t use it and there are better options.
👍4