Solution: Bad arguments
- [Instructor] Custom annotations work really well with exception handling. It's one of my favorite Python tricks for writing clean enterprise code. For this challenge, you needed to write a new custom exception called NonIntArgumentException. And this is probably the easiest part. You literally don't need to do anything besides define the class, make it extend exception, and then you can just write pass under that. We just need it defined. Then you want to fill in the rest of this wrapper function under here. As you can see, the function sum down here takes in three arguments, but I opted to make the wrapper handle any number of arguments just by iterating through the args tub here. I check each one, make sure it's an integer. If it's not an integer, I raise my NonIntArgument exception. Finally, I make sure to return the function that was passed in with its original arguments. So remember, if you don't return this, then that will mean that some isn't going to return anything, and then the caller is going to be really confused when they don't get a result there. Finally, if everything looks good, let's go ahead and test it. All right, all the test cases pass.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。