Share
Embed
Pythonからみる多相の話
PyCon mini Hiroshima 2016 の発表資料の一部
出てくるコードのソースコード
https://gist.github.com/eiel/089b705820ce1c83de22440a3252029d
Tomohiko Himura
November 11, 2016
Transcript
45 slides
-
from __future__ import print_function class Hello: def say(self): print("Hello") class
Goodbye: def say(self): print("Goodbye") 2つのクラスがあるとします sayメソッドを持つ -
from __future__ import print_function class Hello: def say(self): print("Hello") class
Goodbye: def say(self): print("Goodbye") selfがHelloの時の処理 selfがGoodbyeの時の処理 HelloクラスとGoodbyeクラスは 別のファイルにあっても良い -
def say(self): self.say() def say(self): if isinstance(self, Hello): Hello.say(self) if
isinstance(self, Goodbye): Goodbye.say(self) -
def say(self): self.say() 同じように動くようにメソッドを関数 として使ってみる def say(self): if isinstance(self, Hello):
Hello.say(self) if isinstance(self, Goodbye): Goodbye.say(self) -
from __future__ import print_function class Hello: pass class Goodbye: pass
class Teacher: def __init__(self, name): self.name = name class Friend: def __init__(self, name): self.name = name -
say(Hello(), Teacher("ʹΌΜ͜")) # ͜ΜʹͪɺʹΌΜ͜ઌੜ say(Hello(), Friend("ϞϯςΟ")) # ͋ ϞϯςΟ say(Goodbye(),
Teacher("ʹΌΜ͜")) # ʹΌΜ͜ઌੜɺ͞Α͏ͳΒ say(Goodbye(), Friend("ϞϯςΟ")) # ·ͨͶ ϞϯςΟ -
def say(self, target): if isinstance(self, Hello): if isinstance(target, Teacher): print("͜Μʹͪɻ%sઌੜ"
% target.name) elif isinstance(target, Friend): print("͋ %s" % target.name) elif isinstance(self, Goodbye): if isinstance(target, Teacher): print("%sઌੜɺ͞Α͏ͳΒ" % target.name) elif isinstance(target, Friend): print("·ͨͶ %s" % target.name) -
case class Hello() case class Goodbye() case class Teacher(name: String)
case class Friend(name: String) -
// ݺͼग़ͨ͢ΊͷΠϯλʔϑΣΠε trait Say[A, B] { def say(a: A, b:
B): Unit } // ذΛࣗಈੜ͢ΔͨΊͷॴ object Say{ def say[A, B](a: A, b: B)(implicit obj: Say[A, B]) = { obj.say(a,b) } } -
// ࣮ࡍͷಈ࡞ object SayOps { implicit val helloTeacherSay: Say[Hello, Teacher]
= new Say[Hello, Teacher] { def say(self: Hello, target: Teacher): Unit = { println(s"͓Α͏͍͟͝·͢ɻ${target.name}ઌੜ") } } implicit val helloFriedSay: Say[Hello, Friend] = new Say[Hello, Friend] { def say(self: Hello, target: Friend): Unit = { println(s"͋ ${target.name}") } } implicit val goodbyeTeacherSay: Say[Goodbye, Teacher] = new Say[Goodbye, Teacher] { def say(self: Goodbye, target: Teacher): Unit = { println(s"${target.name}ઌੜɺ͞Α͏ͳΒ") } } implicit val goodbyeFriendSay: Say[Goodbye, Friend] = new Say[Goodbye, Friend] { def say(self: Goodbye, target: Friend): Unit = { println(s"·ͨͶ ${target.name}") } } } 分岐後の処理が独立しているのがポイント (ifなどの分岐の中にない) -
// ࣮ࡍʹಈ͔͢෦ object Main extends App { import Say.say import
SayOps._ say(Hello(), Teacher("ʹΌΜ͜")) // ͜ΜʹͪɻʹΌΜ͜ઌੜ say(Hello(), Friend("ϞϯςΟ")) // ͋ ϞϯςΟ say(Goodbye(), Teacher("ʹΌΜ͜")) // ʹΌΜ͜ઌੜɺ͞Α͏ͳΒ say(Goodbye(), Friend("ϞϯςΟ")) // ·ͨͶ ϞϯςΟ } -
大きく5つの部分がある • A 分岐の要素となるデータの種類の定義 • B インターフェイスの定義 • C 分岐が自動生成される部分
• D 動作を実装する部分 • E 使う部分 -
case class Hello() case class Goodbye() case class Teacher(name: String)
case class Friend(name: String) A 分岐の要素となるデータの種類の定義 -
// ݺͼग़ͨ͢ΊͷΠϯλʔϑΣΠε trait Say[A, B] { def say(a: A, b:
B): Unit } // ذΛࣗಈੜ͢ΔͨΊͷॴ object Say{ def say[A, B](a: A, b: B)(implicit obj: Say[A, B]) = { obj.say(a,b) } } B インターフェイスの定義 C 分岐が自動生成される部分 -
// ࣮ࡍͷಈ࡞ object SayOps { implicit val helloTeacherSay: Say[Hello, Teacher]
= new Say[Hello, Teacher] { def say(self: Hello, target: Teacher): Unit = { println(s"͓Α͏͍͟͝·͢ɻ${target.name}ઌੜ") } } implicit val helloFriedSay: Say[Hello, Friend] = new Say[Hello, Friend] { def say(self: Hello, target: Friend): Unit = { println(s"͋ ${target.name}") } } implicit val goodbyeTeacherSay: Say[Goodbye, Teacher] = new Say[Goodbye, Teacher] { def say(self: Goodbye, target: Teacher): Unit = { println(s"${target.name}ઌੜɺ͞Α͏ͳΒ") } } implicit val goodbyeFriendSay: Say[Goodbye, Friend] = new Say[Goodbye, Friend] { def say(self: Goodbye, target: Friend): Unit = { println(s"·ͨͶ ${target.name}") } } } D 動作を実装する部分 -
// ࣮ࡍʹಈ͔͢෦ object Main extends App { import Say.say import
SayOps._ say(Hello(), Teacher("ʹΌΜ͜")) // ͜ΜʹͪɻʹΌΜ͜ઌੜ say(Hello(), Friend("ϞϯςΟ")) // ͋ ϞϯςΟ say(Goodbye(), Teacher("ʹΌΜ͜")) // ʹΌΜ͜ઌੜɺ͞Α͏ͳΒ say(Goodbye(), Friend("ϞϯςΟ")) // ·ͨͶ ϞϯςΟ } E 使う部分 -
{-# LANGUAGE MultiParamTypeClasses #-} import Text.Printf data Hello =
Hello data Goodbye = Goodbye data Teacher = Teacher String data Friend = Friend String A 分岐の要素となるデータの種類の定義 おまじない -
class Say a b where say :: a ->
b -> IO () B インターフェイスの定義 -
instance Say Hello Teacher where say _ (Teacher name) =
printf "͜Μʹͪɻ%sઌੜ\n" name instance Say Hello Friend where say _ (Friend name) = printf "͋ %s\n" name instance Say Goodbye Teacher where say _ (Teacher name) = printf "%sઌੜɺ͞Α͏ͳΒ\n" name instance Say Goodbye Friend where say _ (Friend name) = printf "·ͨͶ %s\n" name D 動作を実装する部分 -
main = do say Hello (Teacher "ʹΌΜ͜") say Hello (Friend
"ϞϯςΟ") say Goodbye (Teacher "ʹΌΜ͜") say Goodbye (Friend "ϞϯςΟ") E 使う部分 -
class HelloTeacher: def __init__(self, name): self.name = name def say(self):
print("͜Μʹͪɻ%sઌੜ" % self.name) class HelloFriend: def __init__(self, name): self.name = name def say(self): print("͋ %s" % self.name) class GoodbyeTeacher: def __init__(self, name): self.name = name def say(self): print("%sઌੜɺ͞Α͏ͳΒ" % self.name) class GoodeByeFriend: def __init__(self, name): self.name = name def say(self): print("·ͨͶ %s" % self.name) A 分岐の要素となるデータの種類の定義 D 動作を実装する部分 -
σʔλ ΠϯλʔϑΣΠε ੜՕॴ ࣮ 1ZUIPO ඞཁ μοΫλΠϐϯά Ͱෆཁ ඞཁ ඞཁ
4DBMB ඞཁ ඞཁ ඞཁ ඞཁ )BTLFMM ඞཁ ඞཁ ෆཁ ඞཁ