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.
@abebuckingham81986 ай бұрын
Dictionaries are a kind of function and work really well for dispatching.
@abebuckingham81982 жыл бұрын
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]().
@munteanionut39932 жыл бұрын
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 Жыл бұрын
Hell yeah
@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 Жыл бұрын
Would this be preferable to using eval(f”{person_type}()”), and it so, why?
@martinbel6 ай бұрын
great solution! This is better
@manifaridi92003 жыл бұрын
You proved programmers can also have biceps 💪
@raymondmichael49873 жыл бұрын
These pattern tutorials are gud, please we need more and eventually creating a project with one
@mr_robot_tech2 ай бұрын
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.
@arthur69993 жыл бұрын
day two: async/await tutorial
@tips14833 жыл бұрын
Yes please I understand the idea but when trying to use it I lose brain cells
@tips14833 жыл бұрын
@Rudy Rayden mmhm sure I see that you both decided to make your account 3 weeks ago stop.
@bhaveshvaishnav73882 жыл бұрын
Explained with such a simple way without complicating anything. Awesome!!
@kevinjones2382 жыл бұрын
The clarity of your explanation/example of the factory pattern is awesome!!!
@iiStxrii3 жыл бұрын
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
@ptyspawnbinbash3 жыл бұрын
Thanks for all the valuable videos, supported you by buying all the seven books. :)
@pythonisthebest3583 жыл бұрын
You’re unbelievably cool! Keep up the good work!
@ddjambel2 жыл бұрын
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 Жыл бұрын
Great explanation as usual. I'm defintively a rookie on OOP, but I'm learning a lot. Thanks !
@golammuhaimeen28253 жыл бұрын
that was a really nice and simple example man. thank you so much. cleared up my understanding of a factory design pattern
@heroicrhythms8302 Жыл бұрын
instead of hardcoding "student" and "Teacher" values, you can use an ENUM
@deeprony73 жыл бұрын
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?
@hasibislam44213 жыл бұрын
I'm really liking this advanced python tutorials......Can you please make a video describing what meta classes and how they work?
@Custom-Tkinter Жыл бұрын
Which text editor are you using bro???
@dpm-072 жыл бұрын
can anyone tell me difference between Factory Design and Abstract Factory Design ?
@jerielmorales17263 жыл бұрын
Great Tutorial. Part 2 of Vim course next =)
@gauff3r3 жыл бұрын
helpfull, thanks, I'd rethink the use of conditions in factory
@Jovana_bp2 жыл бұрын
thanks for simple and good example
@_indrid_cold_3 жыл бұрын
Awesome! Really good examples and explanations. Thank you!
@rsc82952 жыл бұрын
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!
@wariare12world953 жыл бұрын
building a person sounds scary!
@NeuralNine3 жыл бұрын
>:D
@julietatallaferro20722 жыл бұрын
great teaching :) thank u again!!
@travelcinematically3 жыл бұрын
Explained so beautifully 🤟🤟
@pratheek50962 жыл бұрын
Perfectly explaineddd
@andrejk95232 жыл бұрын
A combination of __Init_subclass__ and __new__ implimented in the master class would be a better implementation
@dmitriyobidin60493 жыл бұрын
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.
@AnIceCrasher3 жыл бұрын
Really well explained! Thank you.
@JeffreyMyersII Жыл бұрын
nicely explained, but i have a question. isn't creating factories a use case that the ___new___ class method was made for?
@naserghodsian55453 жыл бұрын
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?!
@aniketpurohit86133 жыл бұрын
This is a tremendous playlist. Looking for async/await video.
@chongsun787211 ай бұрын
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 Жыл бұрын
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 🙏
@burakhanaksoy3 жыл бұрын
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.
@ishfaqbabar23552 жыл бұрын
Which Code Editor are you using .
@girish6064 Жыл бұрын
Thanks for sharing!
@alperklc77822 жыл бұрын
What is the difference between ABC or ABCMETA class? Can we use one instead of the other? Thanks for the lesson.
@munzutai3 жыл бұрын
What I've never understood is what's wrong with doing it without a factory? What are the benefits of using a factory?
@falklumo Жыл бұрын
You want to hide the subtypes. There is normally no need to expose subtypes to client code. Therefore, a factory is required.
@suche_cz2 жыл бұрын
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]()
@val88013 жыл бұрын
I see your video only for you awesome intro :)
@ytalodossantosamorim30523 жыл бұрын
Are you using the terminal to write a script? If yes, .......how
@andytheodorko98743 жыл бұрын
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_Xavien2 жыл бұрын
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)?
@melokorn2 жыл бұрын
Really useful. Thanks
@Liproqq3 жыл бұрын
Super tutorial. Würdest du empfehlen auf's englische keyboard layout zu wechseln?
@hoangphan24298 ай бұрын
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.
@someonemight2 жыл бұрын
What's the benefit of doing this vs just having a utility function?
@ruslanways2 жыл бұрын
what IDe did you used?
@danielg.campos96622 жыл бұрын
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
@pawepuciennik66043 жыл бұрын
I don't understand, what is it abstract class for? The whole factory would work without it.
@nopens3 жыл бұрын
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 Жыл бұрын
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-weilsicherheitdiew52782 жыл бұрын
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?
@alexd74662 жыл бұрын
no
@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.
@TheDigitalSight3 жыл бұрын
Which fonts do you use?
@Ricocase3 жыл бұрын
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 Жыл бұрын
Why to create a factory class when you can, for instance, create a function to do the same?
@os_.mos_.m93463 жыл бұрын
What is the name of the song you are using in the intro?
@klipkon1941 Жыл бұрын
Why are they static methods?
@cahitskttaramal31522 жыл бұрын
it was a great and simple tutorial could you also more design patterns if possible?
@a1dgha13 жыл бұрын
How come you haven't upgraded your Python to 3.9 yet?
@user-sy1sg4ws3y3 жыл бұрын
This is Simple Factory?
@franciscolucarini87613 жыл бұрын
Hey bro will you do differences between python2 and python3 regarding bytes format, thx in advance
@nopens3 жыл бұрын
Doesnt work in python 3.8.3 if you pass "meta=" instead of "metaclass=".
@pythonicd12393 жыл бұрын
Thanks man! That helped!
@__30933 жыл бұрын
you're getting more viewers
@ninjacodertech3 жыл бұрын
possible tutorial: how to make VPN in python pls try to make
@Pyrografpl2 жыл бұрын
Thank you
@armans4306 Жыл бұрын
Bro can you give. some vim tutorial with python. I really like your setup.
@andytheodorko98743 жыл бұрын
I'd use 'born_person' 8:12
@z4ramoth3 жыл бұрын
more pattern vids please !
@alialavizadeh27753 жыл бұрын
so useful tnx
@ahmedossama77382 жыл бұрын
Great!
@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 Жыл бұрын
nice one
@01akbarikrunal243 жыл бұрын
ya i learn lost of thanks
@sachinpadwalkar84463 жыл бұрын
Please upload your vim plugin setup 🙏
@NeuralNine3 жыл бұрын
It is already on github
@sachinpadwalkar84463 жыл бұрын
@@NeuralNine can you tell me the names of all the plugin that you use ?
@NeuralNine3 жыл бұрын
@@sachinpadwalkar8446 all the names are in the github file? Why do you want me to list them here in the comment section manually?
@sachinpadwalkar84463 жыл бұрын
@@NeuralNine oh found it thanks
@ViralKiller2 жыл бұрын
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 Жыл бұрын
You haven't seen big projects. Python doesn't do big projects for a reason ...
@kaya128923 жыл бұрын
python in minecraft +1
@undergroundbodega9766 Жыл бұрын
Gives and example then said “ this is not factory…”. What the point of that. I wish videos were straight forward.
@tips14833 жыл бұрын
1st
@NeuralNine3 жыл бұрын
yes ^^
@tips14833 жыл бұрын
@@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
@winartoit4 ай бұрын
i dont know
@destiny_023 жыл бұрын
12th
@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!