Hisateru Tanaka
February 28, 2019
54k

# プログラマーって どんな人? -- 牛乳と卵で理解するプログラマーという人種

Around Taisho 『大正GeekNight〜プログラマーたちが大正にやってくる〜』No.11
... のスピーチです

## Hisateru Tanaka

February 28, 2019

## Transcript

3. ### ฼͕ݴ͍·ͨ͠ ʮϋχʔɺࢢ৔΁ߦͬͯڇೕΛළͰ ͭങ͖ͬͯͯɻ΋͠ཛ͕͋ͬͨΒ ͓ͭئ͍ʯ ࢲ͸ͭͷڇೕළΛ࣋ͬͯؼ͖ͬͯ ·ͨ͠ɻ ൴ঁ͸ݴ͍·ͨ͠ʮͳΜͯ͜ͱɺͳ ΜͰڇೕΛຊ΋ങ͖ͬͯͨͷ ʯ ࢲ͸ݴ͍·ͨ͠ʮͩͬͯཛ͕͋ͬͨ

͔Βʯ ϓϩάϥϚʔͱ͸
4. ### from shop import Item, Shop shop = Shop('ΠΦϯ υʔϜγςΟళ') cart

= shop.cart() cart.add(Item.MILK) receipt = shop.paycheck(cart) print(receipt) ࣮ࡍʹ࡞ͬͯΈͨͷͰݕূ͠·͠ΐ͏ ͓ళͷΧʔτʹ঎඼ΛೖΕͯࢧ෷͍Λ͢ΔͱϨγʔτ͕ग़·͢ IUUQTHJTUHJUIVCDPNUBOBLBIJTBUFSV EGFDBBFGDCBGEE

12. ### def buy(shop, item, count): cart = shop.cart() for i in

range(count): cart.add(item) return shop.paycheck(cart) item = Item.MILK count = 1 receipt = buy(shop, item, count) print(receipt) ϓϩάϥϚʔ͸͔͍͜͠ͷͰɺؔ਺Λ࡞Ε·͢

14. ### count = 1 # ௥Ճ࢓༷ ళʹཛ͕͋Δ৔߹͸ߪೖྔΛ 6 ͱ͢Δ if shop.stock_of(Item.EGG)

> 0: count = 6 ϓϩάϥϚʔ͸ਓʹࢥΘΕ͍ͯΔ΄Ͳ࣮ࡍʹ͸ ͔͘͜͠ͳ͍ͷͰɺ͙͢ʹʮ৚݅จʯΛཚ༻͠·͢ ͪͳΈʹʮ͋ͱͦΕ͔Β͜Ε΋ʯͱ࠷ޙʹݴΘΕͨ͜ͱ͸ಛʹ΍Β͔͠·͢
15. ### \$ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 6఺

ʮͩͬͯཛ͕͔͋ͬͨΒʯ ͔ͯ͘͠ʜ
16. ### from shop import Item, Shop def buy(shop, item, count): cart

= shop.cart() for i in range(count): cart.add(item) return shop.paycheck(cart) shop = Shop('ΠΦϯ υʔϜγςΟళ') item = Item.MILK count = 1 # ௥Ճ࢓༷ ళʹཛ͕͋Δ৔߹͸ߪೖྔΛ 6 ͱ͢Δ if shop.stock_of(Item.EGG) > 0: count = 6 receipt = buy(shop, item, count) print(receipt) ಄ͷମૢ Ͳ͕͓͔͍͜͠Ͱ͠ΐ͏͔ ώϯτ͔͜͜ΒԼ

20. ### class Shopping: def __init__(self, shop): self.shop = shop self.cart =

shop.cart() def take(self, item, count): for i in range(count): self.cart.add(item) def paycheck(self): return self.shop.paycheck(self.cart) ΄Μͱ͏ʹ͔͍͜͠ϓϩάϥϚʔ͸ ࢥߟΛิ͏ͨΊʹʮผͷ֓೦ʯΛͻͶΓग़͠·͢ ࣮ࡏ͠ͳ͍ʮങ͍෺͢Δͱ͍͏͜ͱʯΛදݱͯ͠ΈΑ͏ ങ͍෺ͱ͸ ঎඼ΛखʹऔΔ͜ͱ ࢧ෷͏͜ͱ
21. ### shop = Shop('ΠΦϯ υʔϜγςΟళ') shopping = Shopping(shop) shopping.take(Item.MILK, 1) if

shop.stock_of(Item.EGG) > 0: shopping.take(Item.MILK, 6) # FIXME ਖ਼͍͠঎඼͕Կ͔֬ೝͯ͠मਖ਼͢Δ͜ͱ receipt = shopping.paycheck() print(receipt) ͦͷ֓೦Λ࢖ͬͯʮจষͷΑ͏ʹॻ͘ʯͱɺ ύζϧͷ··Ͱ͸ݟ͑ͳ͔ͬͨʮؾ͖ͮʯ͕͋Γ·͢ ͋Ε ͜͜঎඼ͷࢦఆ͕ཁΔͧ
22. ### shopping = Shopping(shop) shopping.take(Item.MILK, 1) if shop.stock_of(Item.EGG) > 0: shopping.take(Item.EGG,

6) ʮಡΊΔʯίʔυ͸͙͢ʹͳ͓ͤ·͢ ཛ͕͋ͬͨͱ͖ങͬͯཉ͍͠ͷ͸ɺڇೕ͡Όͳͯ͘ී௨ཛͰ͠ΐ

ཛ ... 6఺

25. ### \$ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 1఺

ཛ ... 6఺ shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.EGG, 100) ;ͭ͏ϓϩάϥϚʔ͸ɺཛ͕े෼ʹ͋Δ৔߹Λςετ͠·͢ ళʹԿ఺ࡏݿ͕͋Δ͔એݴ
26. ### \$ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ڇೕύοΫ ... 1఺

shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.EGG, 0) ͔͍͜͠ϓϩάϥϚʔ͸ɺཛ͕ͳ͍৔߹΋ςετ͠·͢ ళͷࡏݿΛʹͯ͠ΈΔ
27. ### shop = Shop('ΠΦϯ υʔϜγςΟళ') shop.stocks(Item.EGG, 5) if shop.stock_of(Item.EGG) > 0:

shopping.take(Item.EGG, 6) ͋Ε ཛͷࡏݿ͕ΑΓগͳ͍ͱʜ ΋ͬͱ͔͍͜͠ϓϩάϥϚʔ͸ɺςετ͔ΒϩδοΫΛൃݟ͠·͢ ͭ͋Δ΋ͷΛ ͭͰ΋͋Ε͹ ͭങ͏

29. ### \$ python main.py Traceback (most recent call last): File "main.py",

line 43, in <module> shopping.take(Item.EGG, 6) File "main.py", line 30, in take self.cart.add(item) File “/.../shop.py”, line 19, in add raise Exception("%sͷࡏݿ͕͋Γ·ͤΜ" % item.value) Exception: ཛͷࡏݿ͕͋Γ·ͤΜ ίʔυ͸ؒҧͬͯͳͯ͘΋ɺ͓͔͠ͳϩδοΫͩͱɺ ࣮ߦͨ͠ͱ͖Τϥʔ͕ग़·͢ ʮ͏ΘɺͳΜ͔Ϡόͦ͏ͳͷग़Δ΍Μʯ
30. ### # FIXME ࢓༷όά: 1 ͭͰ΋ߪೖ͢Δʹ͸ࡏݿ͕ඞཁ shopping.take(Item.MILK, 1) # FIXME ࡏݿ͕

1 ʙ 5 ͷͱ͖ΤϥʔͰམͪΔ if shop.stock_of(Item.EGG) > 0: shopping.take(Item.EGG, 6) ࡏݿ͕଍Γͳ͍ͱ͖ߪೖͨ͠Βࢮ͵΍͹͍ ແ৚݅ʹͭങ͖ͬͯͯͱ͔ɺͳΜͯةݥͳ࢓༷ͳΜͩ
31. ### # ߪೖ͢Δʹ͸ࡏݿ͕ඞཁ [hotfix] if shop.stock_of(Item.MILK) >= 1: shopping.take(Item.MILK, 1) #

6 ͭߪೖ͢Δʹ͸ 6 Ҏ্ͷࡏݿ͕ඞཁ [hotfix] if shop.stock_of(Item.EGG) >= 6: shopping.take(Item.EGG, 6) ͬͯͳײ͡Ͱ߄ͯΔͱΖ͘ͳ͜ͱΛ͠·ͤΜ ͔͘͜͠ͳ͍ͷͰ৚݅จΛʜ
32. ### \$ python main.py ⏎ ΠΦϯ υʔϜγςΟళ Ͱͷ͓ങ্͍͛: ͸ ແ shop.stocks(Item.EGG,

5) shop.stocks(Item.MILK, 0)

34. ### stock = shop.stock_of(Item.EGG) if stock > 0: if stock <

6: shopping.take(Item.EGG, stock) else: shopping.take(Item.EGG, 6) ͋Δͱ͖͚ͩങͬͯɺͳ͍ͱ͖͸ങͬͪΌμϝͰʜ ࡏݿ͕ΑΓগͳ͍ͳΒࡏݿͿΜ͚ͩങͬͯ ͦ͏͡Όͳ͔ͬͨΒങͬͯʜ ͜Ε͕ڇೕͱཛʹରͯ͠ඞཁͰʜ ͜͏ͳͬͯ͠·͏ͱటপͰ͢
35. ### class Shopping: def wants(self, item, wanted): stock = shop.stock_of(item) if

stock < 1: # ࡏݿ͕ͻͱͭ΋ͳ͚Ε͹खʹऔΕͳ͍ pass elif stock < wanted: # ࡏݿ͕ཉ͍͠ྔΑΓগͳ͍ͱ͖͸ࡏݿ෼Ͱզຫ͢Δ self.take(item, stock) else: # ࡏݿʹ໰୊͕ͳ͚Ε͹ཉ͍͠෼͚ͩߪೖͰ͖Δ self.take(item, wanted) ͦΜͳෳࡶͳهड़Λɺཁ͢ΔʹʮԿ͕Կݸཉ͍͠ͷ͔ʯͱ ݴ͍׵͑ΒΕΔΑ͏ʹ͢ΔͳͲ͍ͯ͘͠ͱ shopping.wants(Item.EGG, 6) ཛͭཉ͍͠Ͱ͢

item, wanted): ... def purchase(self): ... ʮ঎඼Λཉ͍͠ʯͱࢥ͏ͷ͸ʮߪೖऀʯͩ͠ ʮߪೖऀʯͷ͢Δ͜ͱ͸ʮߪങʯͩΑͶ ʜͱɺ࡞ͬͨ֓೦͕ͩΜͩΜɺݱ࣮Λ໛ͨ͠ײ͡ʹۙ͘ͳΓ·͢ ߪೖऀ͸ ڇೕΛͻͱͭཉ͍͠ͱݴͬͯ ۚΛ෷ͬͨ ໊લΛม͑ͯΈͨ ໊લΛม͑ͯΈͨ