PPAP in Python

I tried PPAP using a special method of Python. I used __add__ and __mul__ to add and multiply strings.

code

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))


#Run 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!!

Recommended Posts

PPAP in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python