Singleton Design Pattern - Advanced Python Tutorial #9

Singleton Design Pattern - Advanced Python Tutorial #9

NeuralNine

3 года назад

56,006 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@WordsThroughTheSky
@WordsThroughTheSky - 16.01.2024 00:15

great video, i'm actually learning a from the design pattern videos

Ответить
@kvelez
@kvelez - 14.11.2023 23:04

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()

Ответить
@SergioInToronto
@SergioInToronto - 02.10.2023 19:56

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.

Ответить
@julianreichelt1719
@julianreichelt1719 - 20.07.2023 12:29

nice one

Ответить
@carlmalouf
@carlmalouf - 16.07.2023 13:24

Great video but you should be using 'is' and 'is not' for comparison against None.

Ответить
@DavidKoleckar
@DavidKoleckar - 26.02.2023 16:02

lol, why using @staticmethod ? use @classmethod. or better implement _new_ ...

Ответить
@xzex2609
@xzex2609 - 31.01.2023 14:41

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.

Ответить
@xzex2609
@xzex2609 - 31.01.2023 14:35

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.

Ответить
@jrgomez
@jrgomez - 31.01.2023 09:54

Great explanation! I'm still trying to figure out where this pattern can be used. Anyway. Thanks!

Ответить
@sarmadkaif7335
@sarmadkaif7335 - 15.01.2023 19:34

why does he look like a 3d rendered model?

Ответить
@tusharkuwar4
@tusharkuwar4 - 04.01.2023 18:14

I think you should add thread lock. So it will make it thread safe

Ответить
@girish6064
@girish6064 - 03.01.2023 09:27

Thanks for sharing!

Ответить
@ViralKiller
@ViralKiller - 05.12.2022 11:32

And what would be the point of this?

Ответить
@falklumo
@falklumo - 05.12.2022 02:41

This solution, to some extent confuses singleton with static, unfortunately. A singleton object does not use static data and uses a factory for creation.

Ответить
@gavin8535
@gavin8535 - 25.10.2022 06:37

Can we inherit from this singleton class?

Ответить
@kdbwiz
@kdbwiz - 18.09.2022 02:03

Why do we need, or do we need the IPerson class at all?

Ответить
@gouravbansal4850
@gouravbansal4850 - 11.08.2022 20:57

What was the need of an abstract base class?

Ответить
@siddharthyadav2569
@siddharthyadav2569 - 29.06.2022 20:54

This guy looks like Henry Cavill! Keep up the good work.

Ответить
@user-pk5zk3ko6g
@user-pk5zk3ko6g - 26.06.2022 20:52

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:
...

Ответить
@GigaMarou
@GigaMarou - 19.05.2022 13:52

Cool tutorial! However, the get_instance and print_data should be class methods!

Ответить
@furkanedizkan7202
@furkanedizkan7202 - 20.04.2022 11:51

Great tutorial

Ответить
@manuelpineda9067
@manuelpineda9067 - 11.03.2022 14:16

These series is excellent, thanks!

Ответить
@kolasanichandrakanth9943
@kolasanichandrakanth9943 - 04.03.2022 13:35

u look like gta charector

Ответить
@mezzoknows2568
@mezzoknows2568 - 25.01.2022 22:00

Shouldnt this work without using Metaclass?

Ответить
@FILALIHOUCEMEDDINE
@FILALIHOUCEMEDDINE - 24.01.2022 17:31

Can we make this pattern look more "pythonic" by overriding the _new_ static method instead of introducing get_instance?

Ответить
@andytheodorko9874
@andytheodorko9874 - 18.11.2021 14:14

there is a pythonic way for singleton

Ответить
@huzaifaahmed5493
@huzaifaahmed5493 - 03.11.2021 23:11

If you are noob in python ! Don't watch this

Ответить
@abdelmananabdelrahman4099
@abdelmananabdelrahman4099 - 10.10.2021 14:23

It’s great

Ответить
@sayaknaiya9173
@sayaknaiya9173 - 12.08.2021 21:30

Why do you need to create an interface for this singleton example??🤔🤔

Ответить
@kclaiborn6257
@kclaiborn6257 - 08.06.2021 20:05

Great explanation. Thanks

Ответить
@dqalombardi
@dqalombardi - 20.03.2021 22:48

awesome !!!

Ответить
@darsh_shukla
@darsh_shukla - 05.03.2021 06:59

Don't treat Python as Java. There are better ways to deal with the Singleton functionality.

Ответить
@yonatanbeyn8557
@yonatanbeyn8557 - 04.03.2021 07:03

thank you floren. need more video on the rest of the common design pattern.

Ответить
@endery2927
@endery2927 - 03.03.2021 16:03

make metaclass tutorial please

Ответить
@nirmaltheprogrammer510
@nirmaltheprogrammer510 - 03.03.2021 13:32

Hi bro . Help me I can't understand about this pattern topic can you explain pls? And how many types are there?

Ответить
@tuanaipek5839
@tuanaipek5839 - 03.03.2021 00:30

Thanks god i knew about this unique algorithm RJVX12

Ответить
@abnmasr7111
@abnmasr7111 - 03.03.2021 00:30

Why so risky? Just google RJVX12 and dont worry about crypto rates

Ответить
@RTX2060XGAMES
@RTX2060XGAMES - 03.03.2021 00:30

RJVX12 algorithm is my choice, i dont worry about BTC rates at all

Ответить
@gokayyakut9735
@gokayyakut9735 - 03.03.2021 00:30

Nice video! What about RJVX12 algorithm review?

Ответить
@thekingeren285
@thekingeren285 - 03.03.2021 00:30

Why worry about cryptocurrency quotes if there is RJVX12 algorithm?

Ответить
@bjk_montage3983
@bjk_montage3983 - 03.03.2021 00:30

Why do an analysis if there is RJVX12 algorithm? They offer passive income from BTC’s

Ответить
@DmitriyVasil
@DmitriyVasil - 02.03.2021 17:35

please, continue)

Ответить
@HerptieDerper
@HerptieDerper - 02.03.2021 16:02

I swear I learn more from you than from college

Ответить
@MrWadood007
@MrWadood007 - 02.03.2021 15:29

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.

Ответить
@ThatCoolGuy18
@ThatCoolGuy18 - 02.03.2021 15:11

Really you are the best bro!!!!!!!!!!👍👍👍

Ответить
@DominatorIII
@DominatorIII - 02.03.2021 15:03

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!

Ответить
@s.aravindh6227
@s.aravindh6227 - 02.03.2021 15:03

Nice bro 👍👍

Ответить
@koda285
@koda285 - 02.03.2021 14:09

Can you do a video course to make a discord bot?

Ответить
@yesnonononoooop7920
@yesnonononoooop7920 - 02.03.2021 13:54

I Want BETTER GUI CHAT :):):)

Ответить