Help us understand the problem. What is going on with this article?

PythonでPPAP

More than 3 years have passed since last update.

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!!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away