Python Tutorial: Duck Typing and Asking Forgiveness, Not Permission (EAFP)

  Рет қаралды 130,939

Corey Schafer

Corey Schafer

Күн бұрын

In this Python Tutorial, we will look at a couple of the aspects of being "Pythonic". If you've never heard the term Pythonic before, basically it means that you are following conventions and coding styles of the Python language in order to write clean and readable code.
In this video we'll specifically be looking at two aspects of being Pythonic, and those are "Duck Typing" and the concept that it is easier to ask forgiveness than permission (EAFP). Let's get started.
The code from this video can be found at:
github.com/Cor...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com...
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python

Пікірлер: 153
@팍준하
@팍준하 4 жыл бұрын
Summary: Duck Typing and EAFP are the fundamentals for a code to be 'Pythonic' Duck Typing is when programmers do not care about exactly what an object is, but only care about whether an object can do the operations that we need it to be. In Corey's example where he created classes 'human' and 'duck' and instance for each, both had the method 'quack' and 'fly'. Therefore, a programmer who does duck typing makes functions that can make use of objects that are of any classes, as long as they support the operations used in the function. (I believe this is partly caused by python's feature that we don't have to set the data type for the input and output of function. For example, Golang, or simply 'GO' is an programming language created by Google, and it requires the programmer to specifically set the input and output data type for function.) This leads to occasional errors, that occur when some objects do not support the operations. To avoid an error, programmers have 2 choices. 1. Avoid it in the first place 2. handle the error The first choice necessitates using an if block. Corey displays an example where he checks whether certain objects have certain attributes, to prevent errors such as 'class ~~~ does not support attribute ~~~'. He also shows an example where a file might could not be open even after chekcingthat is is accessible using an 'isaccessible' method, which could lead to an error. This is called 'Look before you leap', abbreviated as 'LBYL', and 'Asking Permission' from 'Easier to Ask Forgiveness than to ask Permission'. The second choice leads the coder to use 'try' and 'except' block, which handles the error after it has been found. There are reasons that the second method is favored over the first. 1. It is faster. The first method, 'LBYL' is checking whether an object supports an operation. That means the computer has to access and interpret what kind of class or type the object is. Executing the operations after that 'check', will make the computer having to interpret what the object is 'twice'. 'Asking forgiveness' method only has to access the object once, using the if block. This makes the runtime for 'asking forgiveness' faster, becuase it has less workload for the computer to do. 2. It is more readable compared to the first. In the video, Corey shows an example where he had to check whether a dictionary had a number of keys in it. That was indeed lengthy, which made it harder to read. In another example he showed, he had to be sure that a list had more than 6 arguments in order to print an item at index 5. He used an len() method and >= conditional, which was obviously lengthy . Using Try and Except blocks was a relatively concise and clear about what the code was all about. 3. It gives more control over 'unexpected' error. In the last example that Corey showed, 'LBYL' method did take account of instance where the file might not be accessible. However, Corey points out that a file could be closed right away and raise an error. 'LBYL' method has no way of handling this situation, since it is only designed to 'prevent' an error and does not has the ability to confront it. Try and Except blocks on the other hand, has the ability to resolve errors of any kind. Thank you Corey, you are changing the world for what you have done. I believe that your such actions of uploading free education videos are creating a difference in the world. Anyone in the world, whether they have money or not, can educate themselves as long as they have internet. That is the first step towards reaching general rights to education. Thank you!!! I will support you soon when I get a job!
@peggychen3411
@peggychen3411 4 жыл бұрын
팍준하 I have read all of your comments, very useful. Thx
@Aulin001
@Aulin001 4 жыл бұрын
@@peggychen3411 yes me too
@vivekan97
@vivekan97 3 жыл бұрын
Thanks to you and Corey
@SirThane13
@SirThane13 7 жыл бұрын
I've also found in what little I've done that EAFP pushes you towards code with fewer bugs. The exceptions are verbose and easier to diagnose. With it, I'm forced to see everything that doesn't match up. If I check for every little thing (as admittedly I used to), it turns into a scavenger hunt for why something I needed to run didn't. You can fix this by adding logging to every checked function, but that destroys your project with clutter. I've naturally gravitated towards EAFP, but you've put it into words. As always, thank you a thousand times for your excellent instruction.
@hnd4r7
@hnd4r7 7 ай бұрын
Thank you for sharing this duck type philosophy. Ask forgiveness, Not permission. 1. avoid multiple acess 2. avoid double check race condition.
@sosurim
@sosurim 8 жыл бұрын
You have a God given, natural talent for teaching, especially for Python. I can't get enough of your videos. Please, please make as many as you can. I've subscribed and will probably 'like' them all. Thank you! BTW: Can you make another video that details your particular environment for optimal debugging Python code with Sublime? What is your solution for doing break points, variable checking, etc.? Thank you again so much.
@coreyms
@coreyms 8 жыл бұрын
Thanks! As far as debugging goes, I usually have Eclipse pulled up if I need to do anything in-depth. If I'm within Sublime then I just stick to simple printing and logging. I'll do a video in the near future on both logging and debugging so people can get an idea of my process there. Thanks again.
@djanthony6662
@djanthony6662 8 жыл бұрын
Coming from PHP, this vid is one of the sweetest 15 mins in my life. Thank you.
@hasibahmad3660
@hasibahmad3660 3 жыл бұрын
I have watched a lot of tutorials on youtube, coursera, udemy on python but when it comes to your tutorial, I just can't skip a minute or speed up the video to finish it quickly. Your video says every minute is important, so, don't skip, just learn... Such an eloquent teacher you are who has precision as well as profound knowledge
@Makkar-b3v
@Makkar-b3v 3 ай бұрын
Exactly
@rajeshkumarreddy
@rajeshkumarreddy 8 жыл бұрын
I have learned a lot in Python from your videos than my online courses. A ton of thanks to you.
@akashpatel5505
@akashpatel5505 6 жыл бұрын
I have to agree with other commentators. You are awesome at explaining python concepts!
@mcan543
@mcan543 4 жыл бұрын
#KeyTakeAways 7:00 and 10:42 Asking forgiveness, not permission > more proactive and pythonic way
@ericmacharia804
@ericmacharia804 2 жыл бұрын
This is a great video explaining how to use try/except. But one thing to keep in mind, try/except are most useful in catching errors, ie when you need to display errors to users based on an operation. If you go adding these blocks everywhere then a memory intensive op might be executed overloading your system, only for the whole function to be cancelled since some later block of code did not pass
@athisii_ekhe6167
@athisii_ekhe6167 4 жыл бұрын
Corey, you are the best at explaining things.
@alixaprodev
@alixaprodev 4 жыл бұрын
you are the best teaching methodology I have ever seen
@finix7419
@finix7419 2 жыл бұрын
thank you Corey ,learning python is actually enjoyable with a teacher like you
@FamilyGuySweden
@FamilyGuySweden 6 жыл бұрын
I am a newbie to python and it helped me alot, thanks Corey
@marcello4258
@marcello4258 3 жыл бұрын
good point at the end for this atomic transaction. yes this is always tricky if you are not using mutex
@7674Manish
@7674Manish 4 жыл бұрын
I have watched most of your videos and can say you are awesome. Thanks for the contribution.
@oliaquino4049
@oliaquino4049 Жыл бұрын
I finally understood this topic after watching it the 2nd time. and I realized.. this is how I code! as a beginner , I always ask for permission lol. I always thought that it was kinda wrong because its a cluttered way of coding but I also thought that as long as I'm getting the results I want , It's ok. I should probably simplify my code more haha.
@easydatascience2508
@easydatascience2508 Жыл бұрын
You can watch mine too. Python and R playlists, and you can find links to source files in video description.
@markb6193
@markb6193 6 жыл бұрын
Just like to add also. Your videos are fantastic; love the way you explain the concepts underneath. As most of it is hidden these days, you rarely see people explaining what's going on in the background.
@Darth_Pro_x
@Darth_Pro_x 4 жыл бұрын
Using try except is faster than if when no exceptions is raised, and slower when an exception is raised. So a good rule of thumb is to use try when you expect it to work most of the time, and if when you expect it to not work most of the time
@entropyz5242
@entropyz5242 5 жыл бұрын
I like the pythonic way. It’s more badass with no permission needed. If it messes up, it will handle it like a G.
@johnr1875
@johnr1875 5 жыл бұрын
shut the fuck up. youre such a bitch!!!!
@SamOween
@SamOween 4 жыл бұрын
@@johnr1875 who the heck are you?
@johnr1875
@johnr1875 4 жыл бұрын
@BT you are absolutely right for calling me out. one thing that you dont know is that I actually know entropy z. We go to the same school. We were taking the python course on udemy together over the summer to keep sharp over break. haha but you are right for calling me out
@johnr1875
@johnr1875 4 жыл бұрын
@@SamOween im actually good friends with entropy z. we're both engineering students. im a huge fan of python. i was just trolling on his ass. youre right for calling me out lol
@entropyz5242
@entropyz5242 4 жыл бұрын
@john r wtf, I don't know who the hell you are.
@TonyFlexPromo
@TonyFlexPromo 7 жыл бұрын
Your videos are flawless and priceless. I have learned so much new stuff on your channel.
@coreyms
@coreyms 7 жыл бұрын
Awesome. That's good to hear. Thanks!
@simonolofsson7488
@simonolofsson7488 5 жыл бұрын
Thank you for this video, Corey. I've been coding Python for almost two years and just realized I've been duck typing without knowing it. I still recognize the anti-patterns from my development in C. Putting names on these patterns made it super clear.
@thunder____
@thunder____ Жыл бұрын
I'm glad I was already aware of how new f-strings are before discovering your channel, bc if I didn't know f-strings didn't exist yet at the time you made this video, I'd be very confused at around 7:36 lol
@meosh930
@meosh930 4 жыл бұрын
6:42 The reason we got all the attributes checked is because the "bark" attribute is located at the last line in try. What's a good way to ensure everything in try block runs and gets checked, in real life where we don't know which one's missing? For example, if we had the below order inside try block, thing.quack() thing.bark() thing.fly() .. Correct me if I'm wrong, but I feel it stops after not finding bark(and therefore moves into exception block) without checking fly?
@anelm.5127
@anelm.5127 3 жыл бұрын
There is no way to catch all errors because as soon as one line raises an error the remaining will be ignored. Typically, the code you execute is consecutive in priority (e.g connect_to_database(), read_data(), write_data(), disconnect()), so if the first thing throws an error the rest does not really matter because it depends on the "things that should happen before". If your commands are not somehow coupled (e.g clean_storage(), update_date(), send_email(), connect_to_database()), then you could use individual try: except: for each function call.
@meosh930
@meosh930 3 жыл бұрын
@@anelm.5127 gotcha. Thanks!
@hongphuongo1862
@hongphuongo1862 6 жыл бұрын
if only KZbin has a "Haha" reaction for your videos and codes. Very funny and attractive way to teach programming skills.
@jonathan3488
@jonathan3488 2 жыл бұрын
Great presentation about EAFP. I personally rather use type hinting with the typing module, to make it less error prone
@Ninja-iq2xt
@Ninja-iq2xt 7 жыл бұрын
Thanks for explaining such concepts Corey, You should better know that their isn't anyone who is teaching as good as you. Your channel could just take off once it gets visibility on youtube. Loved your channel!
@AmeerulIslam
@AmeerulIslam 3 жыл бұрын
Brilliant explanation! Best Python teacher!
@samchan2535
@samchan2535 8 жыл бұрын
Thanks,Schafer. It 's been a great help for me.
@alecdelu83
@alecdelu83 3 жыл бұрын
When Corey says "Now you may be wondering..." I'm like hold your horses, I may be understanding just yet.
@cjromb
@cjromb 4 жыл бұрын
Race Condition example: I admit, I didn't look at ALL the comments to see if this was discussed, and I'm definitely NOT arguing about if statements vs. try...I'm coming from a background where race conditions are a BIG deal that ran the entire design of the system...and I'm also just switching over to Python from another language. Anyway...In the race condition example...you mention that the small amount of time between the IF and and the OPEN/READ in the Non-Pythonic way could mean that the file that LOOKED like it was available is suddenly NOT available. So I'm thinking about this...I can immediately think of several scenarios where if you just immediately read the file, like in your Pythonic example....but then that same file disappeared in that same micro amount of time....it might be a problem that you've already read (and may be processing) a file that just disappeared or got locked/inaccessible for one reason or another. What happens when it goes to read, and the file has been locked? I'm going to see if I can reproduce that as a test. In general, I'm curious about what to do to avoid that kind of situation, but still code in a pythonic manner. My whole life is about EAFP....so definitely can appreciate code like that. LOL Also, I have sooo many of your videos in my queue! They're fantastic!
@patrickmullan8356
@patrickmullan8356 7 жыл бұрын
The only problem I see here, is that I have to know all Error-Types, now. Meaning, which error (or its corresponding literal) do I have to catch in the exception part. Is there a easy way to solve this? Or a concise look-up table, somewhere
@intrepidsouls
@intrepidsouls 6 жыл бұрын
I usually throw errors manually, i.e. commit errors deliberately so that the compiler complains and throws an exception with its name.
@alopexcheung2525
@alopexcheung2525 5 жыл бұрын
Maybe you can look for this www.tutorialspoint.com/python/python_exceptions.htm .
@TrumanBurbonk
@TrumanBurbonk 5 жыл бұрын
I wanted to ask this question myself. We'll see what is going to be easier - to commit errors deliberately or to recognize them from the list.
@mirham3802
@mirham3802 5 жыл бұрын
I think it's somehow obvious which exception to look after as long as you know your data type you're dealing with. I mean if you're using collections, try to pay attention to index and keys errors and so on
@MrRyanroberson1
@MrRyanroberson1 3 жыл бұрын
try / except Exception as e; the 'Exception' covers every exception besides the user attempting to halt the program, and is a superclass of all other exceptions (again besides a 'halt the program' exception)
@xaco56
@xaco56 4 жыл бұрын
Another way to think of it is that the library will be doing those checks anyway. If you put in checks of your own, you are duplicating the library code, and the checks will be done twice.
@codecobber1107
@codecobber1107 7 жыл бұрын
Superb as usual, Thanks again Corey
@meunomejaestavaemuso
@meunomejaestavaemuso 7 жыл бұрын
Nice video But I would like to point a small mistake. When you say that you are throwing some error, what you actually mean is catching it. Since you are using the `except SomeError`. Throwing the error would use the `raise SomeError`. When you except the error you are asking forgiveness, but when you throw the error you are not. a = 'a' b = 1 try: print(a + b) except TypeError: print('Forgive-me for trying to add an string to an int') if isinstance(a, str) and isinstance(b, int): raise TypeError('ERROR! You can\'t add string to ints!') else: print(a + b) ########################################## Forgive-me for trying to add a string to a int Traceback (most recent call last): File "", line 10, in raise TypeError('ERROR! You can\'t add string to ints!') TypeError: ERROR! You can't add string to ints!
@peterye1666
@peterye1666 3 жыл бұрын
I think Corey means that you are allowing Python to throw an error. For example, if I have code like this: nums=[1,2,3] print(nums[10]) then I am allowing Python to throw an IndexError. At 14:02, Corey says, "we can't access the file anymore so then when we try to open it then we're going to THROW an error and we're likely not going to CATCH that error because we thought that we could access the file" So when Corey says "we're going to throw an error" he actually means "Python is going to throw an error." I agree that he could have worded this more clearly.
@cryptoTeacherAcademey
@cryptoTeacherAcademey 6 жыл бұрын
The video tutorials is the best and I really appreciate the fact that your are a good teacher and you make complex things easy to understand. Thanks very much!!!!
@randomguy75
@randomguy75 7 жыл бұрын
bloody fucking perfect is every fucking video on this channel. thanks mate.
@heshankumarasinghe3159
@heshankumarasinghe3159 4 жыл бұрын
Thank you.... Learnt new stuff.... Gonna watch this vid again....
@HugoDoucet
@HugoDoucet 8 жыл бұрын
Thank you for sharing this philosophy. This has been a great help for me.
@sergeyb.3502
@sergeyb.3502 4 жыл бұрын
From now on I'm going to go pythonic with my wife. EAFP.
@bellamvishnuvardhanreddy5828
@bellamvishnuvardhanreddy5828 4 жыл бұрын
make sure to add finally block
@milindyadav7703
@milindyadav7703 3 жыл бұрын
Nah dude u r missing the point ...in python it is to make things faster....while that may not be the case with ur wife :p
@angelcaru
@angelcaru 3 жыл бұрын
@@milindyadav7703 r/woooosh
@MichaelLeo
@MichaelLeo 3 жыл бұрын
don't forget about the "duck" type after you try that!!
@ExplorerDheeraj9271
@ExplorerDheeraj9271 4 жыл бұрын
Thank you so much.....loving your teaching...😍😍
@SeekHeart
@SeekHeart 8 жыл бұрын
another example of pointing out something not pythonic is not using a list comprehension when it's appropriate.
@coreyms
@coreyms 8 жыл бұрын
+SeekHeart Great point. I'll continue creating videos on being Pythonic and show some more common examples.
@VISHALROHRA_
@VISHALROHRA_ 7 жыл бұрын
Eagerly waiting
@thomasslone1964
@thomasslone1964 2 жыл бұрын
i just remembered, this is why ive given up writing python code like 6 times before i could really learn it, bro its rediculous how many ways i can say the same expressions in ruby
@Petemackenshaw
@Petemackenshaw 2 жыл бұрын
In the quack() fly() bark() example, the code worked as expected since it was the last method ('bark') that was missing in each case. But we can't be sure of that. What if 'bark' was the first method call in the sequence or quack was the missing method from the class. In that case none of the methods would get invoked but ideally we would still want the two existing methods on the class to be invoked. Is there a better way of tackling this issue other than adding the try/except to each method call individually? Thanks in advance!
@xschen4045
@xschen4045 5 жыл бұрын
I guess I'm gonna be Pythonic from now on.
@briskwalk1100
@briskwalk1100 Жыл бұрын
really useful thing, I didn't know that, thank you!
@rakeshkumar-jk4lt
@rakeshkumar-jk4lt 5 жыл бұрын
Duck flew over my head, highly technical.
@vsabinat
@vsabinat 3 жыл бұрын
Excellent explanation
@JuniorBloxHD
@JuniorBloxHD 4 жыл бұрын
At 4:52, you have code that checks two conditions with a nested if statement. Couldn't you use and instead?
@benurm2390
@benurm2390 3 жыл бұрын
Yes.
@hanjiali1
@hanjiali1 4 жыл бұрын
Well, the file needs to be closed at the end of the video to prevent error, although it went exception.
@johnny_silverhand
@johnny_silverhand 7 жыл бұрын
Nice and clear explanation
@muntadher8087
@muntadher8087 2 жыл бұрын
Thank you so much! this really helped me, thank you so much
@catatonico123
@catatonico123 8 жыл бұрын
Another great explanation, thanks
@rambo5004
@rambo5004 2 жыл бұрын
Can you please make a video on the typing module? Specifically, im pretty lost when it comes to generics
@bartuslongus
@bartuslongus 2 жыл бұрын
Perfect. Thank you.
@rafaelbraschi
@rafaelbraschi 4 жыл бұрын
Very good... But why using the 'else' on this last try/except? Its not be a better aproach to leave all the code on the 'try'?
@SeaDadLife
@SeaDadLife 7 жыл бұрын
Great video! Thanks for sharing your wisdom. I get the advantages of EAFP. However, an object may have quack() and fly() methods yet not fulfill the contract of a Duck object. In large applications would sufficient code in the try-catch block really be more efficient than just using isinstance?
@Lahiru_Udana
@Lahiru_Udana 5 жыл бұрын
Thanks for this great video
@amortalbeing
@amortalbeing 2 жыл бұрын
@14:40, didnt know we can treat except as an if, and if that ends up false, we can use an else after it!!!
@MrBrainOwl
@MrBrainOwl 7 жыл бұрын
Thank you so much for the explanation!!!
@finalsecretofchrono1339
@finalsecretofchrono1339 3 жыл бұрын
So with this specific race condition example, the opening of the file natively prevents the deletion of the file by another function/user?
@satoshinakamoto171
@satoshinakamoto171 4 жыл бұрын
this is really helpful. thanks .
@ligrt2426
@ligrt2426 4 жыл бұрын
hello Corey , i really learned a lot from ur videos , thnx so much. but my question on this video is that the 1st eg of class person and class duck , is it related or same as polymorphism in python? Thank you
@veyselaksin
@veyselaksin 2 жыл бұрын
Thank you so much for this tutorial. I have question that Can I use BaseException for all exception type? Does it make sense?
@panagiotisdeligiannis9610
@panagiotisdeligiannis9610 5 жыл бұрын
Great video, thank you. Unfortunately I was 3 years late and now I 'll just schedule to change my code. :D One question: There are times that instead of just calling two methods, we have to make a big job and the method calls are seperated by some processing time (method0() ----- TIME CONSUMING JOB --- method1() ). Is that a case that we could check if the methods exist prior, so that we can avoid losing time if the methods do not exist?
@MarekSuchomel
@MarekSuchomel 2 жыл бұрын
I have a problem with EAFP. Just looking at the code I cannot see if it will work correct or not, if the catched exception really covers potential error resulting from the previous code.
@royalpranay
@royalpranay 7 жыл бұрын
Thanks for the tutorials. I also have a small question regarding EAFP, What if I don't want my program to stop execution? Shouldn't it be better to "Ask Permission" and if not allowed, the program should continue further execution.
@coreyms
@coreyms 7 жыл бұрын
In Python, catching an exception doesn't mean you need to exit the program. You can do whatever you like... log the error, add an entry to a database, or just ignore it completely with a 'pass' statement. It's up to you.
@ahmedesmaeil6194
@ahmedesmaeil6194 7 жыл бұрын
what is the relation between duck typing and ABC "Abstract Base Classes" ?
@akiratoriyama1320
@akiratoriyama1320 4 жыл бұрын
Thank you!!
@ptf42069
@ptf42069 6 жыл бұрын
Is "assert" considered one of LBYL?
@turboromy
@turboromy 7 жыл бұрын
Got a dumb question. At about 2:00, is there a term to call "quack_and_fly(p)' as opposed to what I expected 'p.quack_and_fly()' ? I'm wondering if it's a new concept or something I missed.
@coreyms
@coreyms 7 жыл бұрын
In that example, we are actually passing each object into a function that runs the quack and fly methods on each object, but the quack_and_fly function itself is NOT a method of either object, therefore we don't run it as a method. It is a separate function that takes an object as an argument. I hope that helps.
@tshaylatte9502
@tshaylatte9502 4 жыл бұрын
thank you once again
@TheSpacecraftX
@TheSpacecraftX 6 жыл бұрын
Yeah I'm not convinced. I'd much rather get an early warning that it's the wrong type than figuring out why a type conversion error happened at runtime several functions deep, using a library I'm not very familiar with. it makes more sense to me to have it checked for you rather than explicitly writing all the checking yourself when a compiler can do it for you. Try catch statements are a thing in static languages so why go to the extra trouble it causes to forego type checking entirely. Is it pythonic to use try catch in Java or C++ or C#?
@rickliles2460
@rickliles2460 3 жыл бұрын
Just getting started wyth python -- Wondering -- could ya parse the thing.__dict__ for the available attributes and create a set based on a filter... then match the set to a dictionary of thingies to determine how to use the object? Kinda like a regex for objects regex for text is one dimensional ... object oriented regex can have N dimensions
@JJ_eats_wings
@JJ_eats_wings 4 жыл бұрын
pure awesome!
@adamwisniewski9386
@adamwisniewski9386 7 жыл бұрын
But what if I need to call these three methods, and calling only one or two of them will mess things up? In that case I would have to check if they all exist before running them, would that still be unpythonic or is there pythonic way to deal with such circumstance?
@markb6193
@markb6193 6 жыл бұрын
Only thing that annoys me about try/catch, is that people use it far too lazily, when they should be using an if.
@TinkCSA
@TinkCSA 7 жыл бұрын
great video! could you zoom in a bit next time? Having only 30 lines in view will be great.
@salrite
@salrite 6 жыл бұрын
Can anyone please explain how are quack and fly attributes, they are methods (functions) right ?
@cahangirove
@cahangirove 7 жыл бұрын
Great video! But I have a question. What about performance? Is try.. except.. method is faster than if.. else.. statements or vice versa?
@g_th1n
@g_th1n 7 жыл бұрын
I think you can have a look at this, stackoverflow.com/questions/1835756/using-try-vs-if-in-python
@shahriarzaman4715
@shahriarzaman4715 6 ай бұрын
Life's easy with static type system. Use traits/interfaces
@shrikantdangi6064
@shrikantdangi6064 8 жыл бұрын
nicely explained
@sukurcf
@sukurcf 5 жыл бұрын
Dude. How did you comment two lines at once? at 7:37
@pisceangs3948
@pisceangs3948 5 жыл бұрын
select all lines that you want to comment and then press Ctrl+/
@sukurcf
@sukurcf 5 жыл бұрын
@@pisceangs3948 I mean those were different lines at different places. Not in sequence.
@phamvantho4981
@phamvantho4981 4 жыл бұрын
@@sukurcf in PyCharm it can be done by holding the Alt key
@hasifkhan8197
@hasifkhan8197 5 жыл бұрын
I'm unable to understand anything in this particular video, In the whole series till now. Maybe it's my first attempt of learning the OOps Concepts.
@mohittheanand
@mohittheanand 7 жыл бұрын
great video. just one question. is my_dict.get('some_attribute') following EAFP
@coreyms
@coreyms 7 жыл бұрын
Sure... if you were to explicitly check if the attribute existed before trying to get it then it would not. It also allows you to return a None or default value if the key doesn't exist, and that is perfectly fine and useful as well.
@mohittheanand
@mohittheanand 7 жыл бұрын
thanks
@DsiakMondala
@DsiakMondala 7 жыл бұрын
Ouch, I'm having a very hard time with this duck... My code is not optimized to this style at all, I feel like I must learn a new way to define classes and functions before this trumps over classic coding. The worst is not knowing what the functions wants and not knowing what it will return to me. Going back to read the class is proving to be very time consuming.
@sudipta_samanta
@sudipta_samanta 6 жыл бұрын
Can you please explain this line: print("I'm {name}. I'm {age} years old and I am a {job}".format(**person)) how unpacking of dictionary values works ?
@coreyms
@coreyms 6 жыл бұрын
Basically, when you unpack a dictionary, you can imagine it passing in all the keys and values as key/value pairs to the format method. So for example, when you unpack, in the background you can think of it doing something like: print("I'm {name}. I'm {age} years old".format(name='Corey', age=30))
@sudipta_samanta
@sudipta_samanta 6 жыл бұрын
Corey Schafer Thank you for explanation. Can we use this unpacking in other cases like list or tuple ?
@glenbenton2175
@glenbenton2175 4 жыл бұрын
Isn't the statement "with f:" on 14:22 unnecessary here?(no-race condition)
@ivandrofly
@ivandrofly 7 жыл бұрын
thank you
@MatiasEzelQ
@MatiasEzelQ 8 жыл бұрын
Could you do a video explaining how to use python base api? PD: Great videos!
@8w494
@8w494 6 жыл бұрын
To me this seems very dangerous. I prefer static typing for this reason.
@michaelholding5469
@michaelholding5469 5 жыл бұрын
I suppose the obvious pitfall with this approach is that you might unexpectedly find that due to a minor error you have just overwritten a file that contains valuable data?
@rajeshraut2955
@rajeshraut2955 5 жыл бұрын
Please increase your font size
@bulldawg4498
@bulldawg4498 5 жыл бұрын
In the Java OOP world, polymorphism and interfaces take the place of "duck typing" ...
@goodboy9413
@goodboy9413 3 жыл бұрын
Hey duck! Please fly while quacking!
@ZzZ-km2bh
@ZzZ-km2bh 3 жыл бұрын
Can please anybody tell me, what is the keyboard combination to comment/uncomment a block of code? It really kills me. thanks!!
@jvsnyc
@jvsnyc 3 жыл бұрын
Highlight Ctrl-/ is the most common, but varies by editor/IDE.
@ZzZ-km2bh
@ZzZ-km2bh 3 жыл бұрын
@@jvsnyc Thanks, its a huge help :)
@jvsnyc
@jvsnyc 3 жыл бұрын
@@ZzZ-km2bh please do good in the world with the millions of hours u eventually save!
@IsaacC20
@IsaacC20 6 жыл бұрын
The mnemonic, EAFP, is non intuitive imo. Long story short: Prefer try/catch/else blocks to checks (type checks, value checks) to handle invalid inputs to functions.
@Xhisorz1
@Xhisorz1 6 жыл бұрын
How to learn duck typing and more in 15 minutes
@malharjajoo7393
@malharjajoo7393 7 жыл бұрын
The explanation was good but felt the motivation wasnt clear , like why we would like to take this approach
@SirThane13
@SirThane13 7 жыл бұрын
When the concepts of Corey's examples are expanded out to the scale of full projects, EAFP results in less file and object process time and produces more clear and readable code.
@malharjajoo7393
@malharjajoo7393 7 жыл бұрын
thanks for that
@mokopa
@mokopa 5 жыл бұрын
coming from a C/java background, I must admit that the EAFP approach seems like a lazy, not-my-problem way of coding. Write robust code, not 'readable' code. If your code is well-written and doesn't need constant fixing, it doesn't need to be 'readable' because no-one is going to need to read it. Constant checks avoid accumulated errors
When u fight over the armrest
00:41
Adam W
Рет қаралды 32 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 30 МЛН
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 122 М.
Why Everyone is always Talking About Duck Typing in Python.
9:42
Python for Everyone
Рет қаралды 3,1 М.
Modern Python logging
21:32
mCoding
Рет қаралды 203 М.
Python Typing - Type Hints & Annotations
24:46
Tech With Tim
Рет қаралды 116 М.
Python Tutorial: Context Managers - Efficiently Managing Resources
20:37
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 110 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 452 М.
Python Tutorial: Logging Advanced - Loggers, Handlers, and Formatters
19:47
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 415 М.
When u fight over the armrest
00:41
Adam W
Рет қаралды 32 МЛН