Before, after practicing Ruby, I posted an article on my blog called Writing FizzBuzz with errors. However, there was a surprising reaction, and the following story was written in Qiita:
-FizzBuzz with errors in Python -Fizzbuzz with exception handling
I'm originally from Python, so I wrote the code quickly in the spirit of "By the way, I haven't written Python for a long time, my skill will be dull, and I'm the person who wrote in the original article, so MOTTAINAI !!" Saw.
However, since the pattern has already been decided within me, it took about 20 minutes, so it may be a good idea to try it as a guide (for those who are confident in Python).
class FizzBuzzError(Exception):
def __init__(self, message, errors):
self.message = message
self.errors = errors
def fizzbuzz_function(n, message):
def _return_fizz(i):
try:
1 / (i % n)
return ''
except ZeroDivisionError:
raise FizzBuzzError(message, message)
return _return_fizz
def error_message(f, *args, **kargs):
def _f(n):
try:
return f(n)
except FizzBuzzError as e:
return e.message
return _f
@error_message
def fizz(n):
return fizzbuzz_function(3, "Fizz")(n)
@error_message
def buzz(n):
return fizzbuzz_function(5, "Buzz")(n)
def fizzbuzz(n):
return "".join([fizz(n), buzz(n)]) or n
def main():
for i in [fizzbuzz(n + 1) for n in range(99)]:
print(i)
if __name__ == "__main__":
main()
This is a Python3 specification. If you are using Python2, please use ʻimport future from print_function``;) `
I can't say anything because I haven't pursued deeply because I'm a little away from Python, but I'm wondering if such an implementation is okay. For the time being, I am using this StackOverflow without understanding it, so maybe This implementation may be redundant.
class FizzBuzzError(Exception):
def __init__(self, message, errors):
self.message = message
self.errors = errors
What makes Python a little better than Ruby is the ease of writing closures? To be honest, I honestly think that it is troublesome to make a proc and hoge in Ruby, and if you do not add ()
in Python, I personally prefer the specification of handling function objects (Rubyist Masakari welcomed) ……!).
By the way, fizzbuzz is abstracted to ** if it is a multiple of something, it issues a message corresponding to it **. If you take out only that part and make it, it is convenient to use closures in Python. As an actual usage example, it looks like the following.
def fizzbuzz_function(n, message):
def _return_fizz(i):
try:
1 / (i % n)
return ''
except ZeroDivisionError:
raise FizzBuzzError(message, message)
return _return_fizz
def fizz(n):
return fizzbuzz_function(3, "Fizz")(n)
Now, let's think further. We want an error hyperreturn, we don't want an error.
If you understand closures, you should try using decorators next. The definition of the decorator is quoted from the following explanation of PEP 318 because it is the easiest to understand:
@dec2
@dec1
def func(arg1, arg2, ...):
pass
This is equivalent to:
def func(arg1, arg2, ...):
pass
func = dec2(dec1(func))
Here in this code:
def error_message(f, *args, **kargs):
def _f(n):
try:
return f(n)
except FizzBuzzError as e:
return e.message
return _f
" "
is False!This is a controversial point, but " "
is False in the Python specification. In other words, if the join of the two results is an empty string, it is already False, so all you have to do is return the received number as it is. It seems convenient because it can be written only with the so-called ʻor operator`! I don't need a ternary operator!
def fizzbuzz(n):
return "".join([fizz(n), buzz(n)]) or n
So, this time I wrote about the error FizzBuzz by Python based on the unexpected reaction.
However, since the original article is used as a Super return, I think it is most likely to point out that raise
is no different from just return
in this case. When I tried to emphasize the Python-ness of decorator
and its surroundings, I ended up with the above code.
Now, what if the language doesn't have error handling?
For example, I remember that Go doesn't have a syntax dedicated to error handling (I know that panic
and recover
are available ...), but in Haskell. There are times when you want to see something;)
Recommended Posts