Factory Design Pattern - Advanced Python Tutorial #7

  Рет қаралды 103,504

NeuralNine

NeuralNine

Күн бұрын

Пікірлер: 109
@alexd7466
@alexd7466 2 жыл бұрын
That is probably how you would do that in Java. But in Python we just use a function that returns the object. You can already see that because you made it a staticmethod, and a class with just one function (and even worse a staticmethod) , is a clear indicator that it should be a function, not a class.
@abebuckingham8198
@abebuckingham8198 6 ай бұрын
Dictionaries are a kind of function and work really well for dispatching.
@abebuckingham8198
@abebuckingham8198 2 жыл бұрын
For factories I prefer to use a dictionary with the key strings and the value being the class in the build_person to avoid the cost of comparison as follows def build_person(person_type): PERSONTYPES = {"Student":Student, "Teacher":Teacher} return PERSONTYPES[person_type]().
@munteanionut3993
@munteanionut3993 2 жыл бұрын
I think behind the scenes there is still a comparison being made - in basically the way indexing a dictionary (ie: PERSONTYPES['some_key_string']) works. But yeah, doing it like you looks prettier to me, too
@breakdancerQ
@breakdancerQ Жыл бұрын
Hell yeah
@PeaceToUsAll
@PeaceToUsAll Жыл бұрын
@@munteanionut3993 no comparison there, it is just the relation between a dictionary (PERSONTYPES) and its key (person_type) dictionaries have a complex but time efficient method of fetching values (using hashes), you can read more on that.
@srs32896
@srs32896 Жыл бұрын
Would this be preferable to using eval(f”{person_type}()”), and it so, why?
@martinbel
@martinbel 6 ай бұрын
great solution! This is better
@manifaridi9200
@manifaridi9200 3 жыл бұрын
You proved programmers can also have biceps 💪
@raymondmichael4987
@raymondmichael4987 3 жыл бұрын
These pattern tutorials are gud, please we need more and eventually creating a project with one
@mr_robot_tech
@mr_robot_tech 2 ай бұрын
I was trying to understand this topic since past one week and watching multiple videos was making me more and more confused. I understand it through this video. Thank you for explaining so well in this video.
@arthur6999
@arthur6999 3 жыл бұрын
day two: async/await tutorial
@tips1483
@tips1483 3 жыл бұрын
Yes please I understand the idea but when trying to use it I lose brain cells
@tips1483
@tips1483 3 жыл бұрын
@Rudy Rayden mmhm sure I see that you both decided to make your account 3 weeks ago stop.
@bhaveshvaishnav7388
@bhaveshvaishnav7388 2 жыл бұрын
Explained with such a simple way without complicating anything. Awesome!!
@kevinjones238
@kevinjones238 2 жыл бұрын
The clarity of your explanation/example of the factory pattern is awesome!!!
@iiStxrii
@iiStxrii 3 жыл бұрын
Hey man, I know you don't have a lot of views yet, but when programming becomes a core subject in schools, you'll be the khan academy of the future. Keep on the grind G
@ptyspawnbinbash
@ptyspawnbinbash 3 жыл бұрын
Thanks for all the valuable videos, supported you by buying all the seven books. :)
@pythonisthebest358
@pythonisthebest358 3 жыл бұрын
You’re unbelievably cool! Keep up the good work!
@ddjambel
@ddjambel 2 жыл бұрын
You need always self in a object, because self, it the object itself, to use all attributes or methods, over "self.". Cheers from Germany.
@jrgomez
@jrgomez Жыл бұрын
Great explanation as usual. I'm defintively a rookie on OOP, but I'm learning a lot. Thanks !
@golammuhaimeen2825
@golammuhaimeen2825 3 жыл бұрын
that was a really nice and simple example man. thank you so much. cleared up my understanding of a factory design pattern
@heroicrhythms8302
@heroicrhythms8302 Жыл бұрын
instead of hardcoding "student" and "Teacher" values, you can use an ENUM
@deeprony7
@deeprony7 3 жыл бұрын
Having the if-else logic still reside in the ConcreteFactory violates the Open-Closed Principle. Would it be better to have an interface for FactoryPerson and implement Student and Teacher from it? If so, how would that implementation look like?
@hasibislam4421
@hasibislam4421 3 жыл бұрын
I'm really liking this advanced python tutorials......Can you please make a video describing what meta classes and how they work?
@Custom-Tkinter
@Custom-Tkinter Жыл бұрын
Which text editor are you using bro???
@dpm-07
@dpm-07 2 жыл бұрын
can anyone tell me difference between Factory Design and Abstract Factory Design ?
@jerielmorales1726
@jerielmorales1726 3 жыл бұрын
Great Tutorial. Part 2 of Vim course next =)
@gauff3r
@gauff3r 3 жыл бұрын
helpfull, thanks, I'd rethink the use of conditions in factory
@Jovana_bp
@Jovana_bp 2 жыл бұрын
thanks for simple and good example
@_indrid_cold_
@_indrid_cold_ 3 жыл бұрын
Awesome! Really good examples and explanations. Thank you!
@rsc8295
@rsc8295 2 жыл бұрын
You could actually add to that to create an automated conversation, like a code people could use to find details of a student or teacher. You would need to add the next prompts and conditionals and include perhaps some code to read in data from file, it would take a lot of time for all the possible responses but be really cool. In fact might do this as practice for my exam. Otherwise cool tutorial!
@wariare12world95
@wariare12world95 3 жыл бұрын
building a person sounds scary!
@NeuralNine
@NeuralNine 3 жыл бұрын
>:D
@julietatallaferro2072
@julietatallaferro2072 2 жыл бұрын
great teaching :) thank u again!!
@travelcinematically
@travelcinematically 3 жыл бұрын
Explained so beautifully 🤟🤟
@pratheek5096
@pratheek5096 2 жыл бұрын
Perfectly explaineddd
@andrejk9523
@andrejk9523 2 жыл бұрын
A combination of __Init_subclass__ and __new__ implimented in the master class would be a better implementation
@dmitriyobidin6049
@dmitriyobidin6049 3 жыл бұрын
Good tutorial, but missing one part that can be helpful - factory also can be instantiated from IFactory interface, cause in some cases we need different concrete factories, depending on user input.
@AnIceCrasher
@AnIceCrasher 3 жыл бұрын
Really well explained! Thank you.
@JeffreyMyersII
@JeffreyMyersII Жыл бұрын
nicely explained, but i have a question. isn't creating factories a use case that the ___new___ class method was made for?
@naserghodsian5545
@naserghodsian5545 3 жыл бұрын
In an statically-typed language like Java you make an Interface to have control over the output of builder method in the factory class. Given the fact that in Python the builder method of the factory class can return anything, what is the point of having an abstract class here?!
@aniketpurohit8613
@aniketpurohit8613 3 жыл бұрын
This is a tremendous playlist. Looking for async/await video.
@chongsun7872
@chongsun7872 11 ай бұрын
One quick question is that, in the Person object, the person_method is an abstract Static method. In student and teacher object, you can rewrite it as a normal method? Is this allowed in Python?
@garfieldgray6746
@garfieldgray6746 Жыл бұрын
Thanks for your brilliant video, I've got a question, In the last lines of your code, "person" is a class that we've determined dynamically, then must be able to instantiate an object with it right?! Like pp=person() But I just can't:/ Do you know why?! Thanks 🙏
@burakhanaksoy
@burakhanaksoy 3 жыл бұрын
Thanks for the tutorial. A personal comment is instead of making if check inside a single @staticmethod, it'd be better to create multiple static methods. In this way if you want to create another person, say engineer, you wouldn't have to make an if check.
@ishfaqbabar2355
@ishfaqbabar2355 2 жыл бұрын
Which Code Editor are you using .
@girish6064
@girish6064 Жыл бұрын
Thanks for sharing!
@alperklc7782
@alperklc7782 2 жыл бұрын
What is the difference between ABC or ABCMETA class? Can we use one instead of the other? Thanks for the lesson.
@munzutai
@munzutai 3 жыл бұрын
What I've never understood is what's wrong with doing it without a factory? What are the benefits of using a factory?
@falklumo
@falklumo Жыл бұрын
You want to hide the subtypes. There is normally no need to expose subtypes to client code. Therefore, a factory is required.
@suche_cz
@suche_cz 2 жыл бұрын
Thank you for video: Is possible to mix interface class with factory class ? eg.: It works, just if it''s not some bad practise. class Person(metaclass=ABCMeta): @abstractmethod def person_method(self): """ Interface method """ @staticmethod def create(person_type): person_class = { 'Student': Student, 'Teacher': Teacher, } return person_class[person_type]()
@val8801
@val8801 3 жыл бұрын
I see your video only for you awesome intro :)
@ytalodossantosamorim3052
@ytalodossantosamorim3052 3 жыл бұрын
Are you using the terminal to write a script? If yes, .......how
@andytheodorko9874
@andytheodorko9874 3 жыл бұрын
Wouldn't it be a better example if the logic of creation of a Person in a Factory be more complex? (add random, multiple parameters)
@A_Xavien
@A_Xavien 2 жыл бұрын
In your example it's more like factory method than just factory if `PersonFactory` would have other actual methods. Also why use `if else` (in `build_person` method), when you can just create some dict that store name-class relationships (which name corresponds to which class)?
@melokorn
@melokorn 2 жыл бұрын
Really useful. Thanks
@Liproqq
@Liproqq 3 жыл бұрын
Super tutorial. Würdest du empfehlen auf's englische keyboard layout zu wechseln?
@hoangphan2429
@hoangphan2429 8 ай бұрын
I love your videos, but I think the example code for the abstract class should be improved. The code doesn't represent the implementation of an abstract class. In PersonFactory class, it basically returns the class.
@someonemight
@someonemight 2 жыл бұрын
What's the benefit of doing this vs just having a utility function?
@ruslanways
@ruslanways 2 жыл бұрын
what IDe did you used?
@danielg.campos9662
@danielg.campos9662 2 жыл бұрын
Thanks so much for the tutorial, btw, i have got this message in the pyCharm : "abc.abstractstaticmethod' is deprecated since Python 3.3. Use 'staticmethod' with 'abc.abstractmethod' instead" so i have to replace it with : "from abc import ABCMeta, abstractmethod" and then in the class : class IPerson(metaclass=ABCMeta): @staticmethod" and it did the work. Any comments on that? thanks again
@pawepuciennik6604
@pawepuciennik6604 3 жыл бұрын
I don't understand, what is it abstract class for? The whole factory would work without it.
@nopens
@nopens 3 жыл бұрын
This. I get it you might wanna double check that every subclass of IPerson has its own imlementation of person_method. Maybe its useful when you have too many subclasses to keep track of them and making the base person abstract would raise an error, maing sure you can safely call that method on every child?
@macieks3604
@macieks3604 Жыл бұрын
And why is it necessary to use abstract method here? It would also be possible to have one base class and 2 child classes with same method name overriding the method of the parent class, so called polymorphism. There is no need for an abstract class here. I've seen many examples of an abstract class used in that way but I still don't understand why it was used....
@pegasus-weilsicherheitdiew5278
@pegasus-weilsicherheitdiew5278 2 жыл бұрын
How can i ensure that an other coder uses the interface to create objects and not the subclass directly? Can i set a class to private for other modules? Like private or public methods?
@alexd7466
@alexd7466 2 жыл бұрын
no
@falklumo
@falklumo Жыл бұрын
The factory could use a secret to initialize the subclass not available public. Unfortunately, Python as a language is not expressive enough to support all the famous design patterns directly.
@TheDigitalSight
@TheDigitalSight 3 жыл бұрын
Which fonts do you use?
@Ricocase
@Ricocase 3 жыл бұрын
Rotate video to see? Video start at 2:10. Where's else for printing error? If input is == "student" print "I'm a student?" What's the main and init?
@rafaeel731
@rafaeel731 Жыл бұрын
Why to create a factory class when you can, for instance, create a function to do the same?
@os_.mos_.m9346
@os_.mos_.m9346 3 жыл бұрын
What is the name of the song you are using in the intro?
@klipkon1941
@klipkon1941 Жыл бұрын
Why are they static methods?
@cahitskttaramal3152
@cahitskttaramal3152 2 жыл бұрын
it was a great and simple tutorial could you also more design patterns if possible?
@a1dgha1
@a1dgha1 3 жыл бұрын
How come you haven't upgraded your Python to 3.9 yet?
@user-sy1sg4ws3y
@user-sy1sg4ws3y 3 жыл бұрын
This is Simple Factory?
@franciscolucarini8761
@franciscolucarini8761 3 жыл бұрын
Hey bro will you do differences between python2 and python3 regarding bytes format, thx in advance
@nopens
@nopens 3 жыл бұрын
Doesnt work in python 3.8.3 if you pass "meta=" instead of "metaclass=".
@pythonicd1239
@pythonicd1239 3 жыл бұрын
Thanks man! That helped!
@__3093
@__3093 3 жыл бұрын
you're getting more viewers
@ninjacodertech
@ninjacodertech 3 жыл бұрын
possible tutorial: how to make VPN in python pls try to make
@Pyrografpl
@Pyrografpl 2 жыл бұрын
Thank you
@armans4306
@armans4306 Жыл бұрын
Bro can you give. some vim tutorial with python. I really like your setup.
@andytheodorko9874
@andytheodorko9874 3 жыл бұрын
I'd use 'born_person' 8:12
@z4ramoth
@z4ramoth 3 жыл бұрын
more pattern vids please !
@alialavizadeh2775
@alialavizadeh2775 3 жыл бұрын
so useful tnx
@ahmedossama7738
@ahmedossama7738 2 жыл бұрын
Great!
@falklumo
@falklumo Жыл бұрын
Shouldn't you mention Erich Gamma et al. and their book "Design Patterns"? After all, the patterns are language-independent and this is just Python trying to catch up 25 years later ;)
@julianreichelt1719
@julianreichelt1719 Жыл бұрын
nice one
@01akbarikrunal24
@01akbarikrunal24 3 жыл бұрын
ya i learn lost of thanks
@sachinpadwalkar8446
@sachinpadwalkar8446 3 жыл бұрын
Please upload your vim plugin setup 🙏
@NeuralNine
@NeuralNine 3 жыл бұрын
It is already on github
@sachinpadwalkar8446
@sachinpadwalkar8446 3 жыл бұрын
@@NeuralNine can you tell me the names of all the plugin that you use ?
@NeuralNine
@NeuralNine 3 жыл бұрын
@@sachinpadwalkar8446 all the names are in the github file? Why do you want me to list them here in the comment section manually?
@sachinpadwalkar8446
@sachinpadwalkar8446 3 жыл бұрын
@@NeuralNine oh found it thanks
@ViralKiller
@ViralKiller 2 жыл бұрын
appreciate the tutorial but completely useless and overcomplicates things for no reason...definitely wouldnt be allowed at my company....way too many dependencies and separate classes for such a simple program
@falklumo
@falklumo Жыл бұрын
You haven't seen big projects. Python doesn't do big projects for a reason ...
@kaya12892
@kaya12892 3 жыл бұрын
python in minecraft +1
@undergroundbodega9766
@undergroundbodega9766 Жыл бұрын
Gives and example then said “ this is not factory…”. What the point of that. I wish videos were straight forward.
@tips1483
@tips1483 3 жыл бұрын
1st
@NeuralNine
@NeuralNine 3 жыл бұрын
yes ^^
@tips1483
@tips1483 3 жыл бұрын
@@NeuralNine funny enough the video took so long to load I originally typed “2nd” but when I realized I was genuinely first I edited it lmao
@winartoit
@winartoit 4 ай бұрын
i dont know
@destiny_02
@destiny_02 3 жыл бұрын
12th
@SergioInToronto
@SergioInToronto Жыл бұрын
Yikes. Bad patterns like crazy. Don't return -1, raise an exception! Class PersonFactory is useless - just write the function! Classes which don't save state are effectively just namespaces, but that's what files are for!
@vascof614
@vascof614 3 жыл бұрын
1st
@tips1483
@tips1483 3 жыл бұрын
Nope beat you
Proxy Design Pattern - Advanced Python Tutorial #8
5:12
NeuralNine
Рет қаралды 36 М.
The Factory Pattern in Python // Separate Creation From Use
14:58
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 2,7 МЛН
Factory Method Pattern - Design Patterns (ep 4)
27:21
Christopher Okhravi
Рет қаралды 553 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 612 М.
Python Abstract Factory Pattern Explained | Object Creation Made Easy
29:09
Python dataclasses will save you HOURS, also featuring attrs
8:50
Design Patterns in Python by Peter Ullrich
27:31
PyCon Sweden
Рет қаралды 134 М.
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 72 М.
Why I Am Still Using Linux - And Won't Change It
20:31
NeuralNine
Рет қаралды 15 М.
The Factory Design Pattern is Obsolete in Python
23:18
ArjanCodes
Рет қаралды 102 М.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 121 М.