5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでPPAP

Last updated at Posted at 2017-01-14

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!!
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?