Комментарии:
great video, i'm actually learning a from the design pattern videos
Ответитьfrom abc import ABCMeta, abstractstaticmethod
class IPerson(metaclass=ABCMeta):
@abstractstaticmethod
def get_data():
"""Implement in child class"""
class SingletonPattern(IPerson):
__instance = None
def __new__(cls, *args, **kwargs) -> None: # new is already a static dunder method.
if not cls.__instance:
cls.__instance = super().__new__(cls)
return cls.__instance
def __init__(self, username):
# if not hasattr(self, "username"): prevents many instances
self.username = username
# else:
# raise Exception("This class is a singleton") notifies error
@staticmethod
def get_data():
print(f"Username: {SingletonPattern.__instance.username}")
if _name_ == "__main__":
person = SingletonPattern("Kevin").get_data()
person = SingletonPattern("John").get_data()
This is an overly-complex solution. Don't need any abstractions or classes. All you need is a global variable + 1 function.
Feels like you're writing Java in Python my dude.
nice one
ОтветитьGreat video but you should be using 'is' and 'is not' for comparison against None.
Ответитьlol, why using @staticmethod ? use @classmethod. or better implement _new_ ...
ОтветитьPython programmers almost never implement the Singleton Pattern as described in the Gang of Four book, whose Singleton class forbids normal instantiation and instead offers a class method that returns the singleton instance. Python is more elegant, and lets a class continue to support the normal syntax for instantiation while defining a custom __new__() method that returns the singleton instance. But an even more Pythonic approach, if your design forces you to offer global access to a singleton object, is to use The Global Object Pattern instead.
ОтветитьA Singleton pattern in python is a design pattern that allows you to create just one instance of a class, throughout the lifetime of a program. Using a singleton pattern has many benefits. A few of them are: To limit concurrent access to a shared resource.
ОтветитьGreat explanation! I'm still trying to figure out where this pattern can be used. Anyway. Thanks!
Ответитьwhy does he look like a 3d rendered model?
ОтветитьI think you should add thread lock. So it will make it thread safe
ОтветитьThanks for sharing!
ОтветитьAnd what would be the point of this?
ОтветитьThis solution, to some extent confuses singleton with static, unfortunately. A singleton object does not use static data and uses a factory for creation.
ОтветитьCan we inherit from this singleton class?
ОтветитьWhy do we need, or do we need the IPerson class at all?
ОтветитьWhat was the need of an abstract base class?
ОтветитьThis guy looks like Henry Cavill! Keep up the good work.
Ответитьimplementation using decorator
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
...
Cool tutorial! However, the get_instance and print_data should be class methods!
ОтветитьGreat tutorial
ОтветитьThese series is excellent, thanks!
Ответитьu look like gta charector
ОтветитьShouldnt this work without using Metaclass?
ОтветитьCan we make this pattern look more "pythonic" by overriding the _new_ static method instead of introducing get_instance?
Ответитьthere is a pythonic way for singleton
ОтветитьIf you are noob in python ! Don't watch this
ОтветитьIt’s great
ОтветитьWhy do you need to create an interface for this singleton example??🤔🤔
ОтветитьGreat explanation. Thanks
Ответитьawesome !!!
ОтветитьDon't treat Python as Java. There are better ways to deal with the Singleton functionality.
Ответитьthank you floren. need more video on the rest of the common design pattern.
Ответитьmake metaclass tutorial please
ОтветитьHi bro . Help me I can't understand about this pattern topic can you explain pls? And how many types are there?
ОтветитьThanks god i knew about this unique algorithm RJVX12
ОтветитьWhy so risky? Just google RJVX12 and dont worry about crypto rates
ОтветитьRJVX12 algorithm is my choice, i dont worry about BTC rates at all
ОтветитьNice video! What about RJVX12 algorithm review?
ОтветитьWhy worry about cryptocurrency quotes if there is RJVX12 algorithm?
ОтветитьWhy do an analysis if there is RJVX12 algorithm? They offer passive income from BTC’s
Ответитьplease, continue)
ОтветитьI swear I learn more from you than from college
ОтветитьHey! A very clear explanation as usual. A suggestion, you could display your video on the bottom left , that way it won't block any text.
ОтветитьReally you are the best bro!!!!!!!!!!👍👍👍
ОтветитьI was missing the explanation, on why you would do this? What are the applications? And why always use person as an example, which doesn't say anything. Couldn't you have used a more applied example? Other than that, I love your videos! Please keep up the good work!
ОтветитьNice bro 👍👍
ОтветитьCan you do a video course to make a discord bot?
ОтветитьI Want BETTER GUI CHAT :):):)
Ответить