Pythonの特殊メソッドを使ってPPAPをやってみました。
文字列の足し算、掛け算を__add__
、__mul__
を使ってやりました。
コード
PPAP
import re
## PPAP class
class PPAP(str):
def __add__(self, other):
self.print_materials(other)
result = "{1}-{0}".format(self, other)
print("Oh, {0}!".format(result))
return PPAP(result)
def __mul__(self, other):
match = re.search("(\w+)-(\w+)", other)
mate_1, mate_2 = match.group(1), match.group(2)
result = "{0}-{1}-{2}".format(mate_2, mate_1, self)
print("{0}!!".format(result))
return PPAP(result)
def print_materials(self, other):
print("I have a {0}~".format(self))
print("I have a {0}~".format(other))
# PPAPの実行
pen = PPAP("pen")
apple = PPAP("apple")
pineapple = PPAP("pineapple")
print("---(1st Phase)---")
result_1 = pen + apple
print("---(2nd Phase)---")
result_2 = pen + pineapple
print("---(Last Phase)---")
result_1 * result_2
result
---(1st Phase)---
I have a pen~
I have a apple~
Oh, apple-pen!
---(2nd Phase)---
I have a pen~
I have a pineapple~
Oh, pineapple-pen!
---(Last Phase)---
pen-pineapple-apple-pen!!