1.Getting Started
Pythonを使ってOOPすなわちObject-Oriented Programmingをやってみます。
2.References
Youtubeとして、
Python OOP Tutorial 1: Classes and Instances by Corey Schafer
また、Documentationとして、
https://docs.python.org/3/tutorial/classes.html
を挙げておきます。
3.Classes and Instances
classesとinstancesの違いは、classesがblueprintである点です。次のcodeをrunしてみてください。
class Employee:
pass
emp_1 = Employee()
emp_2 = Employee()
print(emp_1)
print(emp_2)
[Result]
<__main__.Employee object at 0x0000019FF31D5610>
<__main__.Employee object at 0x0000019FF31D5580>
このように、異なるaddressにinstancesが作られており、instancesは別々のものだとわかります。
4.attributes
今、作ったclass Employeeは、中身が空っぽのclassでした。この何もないclassに、attributesを付け加えてみましょう。次のcodeでは、first, last, email, payという4つのattributesをEmployeeのinstancesであるemp_1とemp_2に、事後的に、それぞれ設定しています。
class Employee:
pass
emp_1 = Employee()
emp_2 = Employee()
print(emp_1)
print(emp_2)
emp_1.first = 'Mark'
emp_1.last = 'Twain'
emp_1.email = '[email protected]'
emp_1.pay = 50000
emp_2.first = 'Test'
emp_2.last = 'User'
emp_2.email = '[email protected]'
emp_2.pay = 60000
print(emp_1.email)
print(emp_2.email)
[Result]
5.__init__()
しかし、このようなやり方の場合、classの中身がflexibleに作られる利点がある一方で、errorを生じやすくなります。そこで、instanceを作成すると同時に、自動的に、attributesを設定していく方法が好まれます。すなわち、class Employee:の直後にpassを書いて空のclassを作ってから、attributesを付け加えていくのではなく、__init__()というmethodを使って、Employee()のかっこの中で、attributesをあらかじめ入力させることで、instancesを作ると同時にattributesも設定する方式をとります。
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
self.pay = pay
emp_1 = Employee('Mark', 'Twain', 50000)
emp_2 = Employee('Test', 'User', 6000)
print(emp_1.email)
print(emp_2.email)
[Result]
先ほどと同じ結果を得ることができました。このように、instanceの名前の直後に、.emailを付けると、class Employeeで設定されたfirst + '.' + last + '@company.com'が返されます。このemailのことをattributeと呼びます。Employeeには、emailのほかに、first, last, payというattributesが設定されています。
ここで、def __init__()のかっこの中では、必ず、self(実は、単語はなんでもいいのですが)というargumentが必要となることに注意してください。これは、emp_1のinstanceを作る場合には、emp_1のaddressを意味し、emp_2のinstanceを作る場合には、emp_2のaddressを意味すると考えられます。
6.methods
class Employeeについて、何かを実行したいとします。たとえば、次のようなことをclass定義の外で実行するとします。
#I want to do some action:
print('{} {}'.format(emp_1.first, emp_1.last))
[Result]
Mark Twain
同じことを、class Employeeの定義の中で行うことができます。それをmethodと呼びます。
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
self.pay = pay
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('Mark', 'Twain', 50000)
emp_2 = Employee('Test', 'User', 6000)
#I want to do some action:
#print('{} {}'.format(emp_1.first, emp_1.last))
print(emp_1.fullname())
print(emp_2.fullname())
[Result]
Mark Twain
Test User
ここでは、class Employeeの定義の中で、
def fullname(self):
return '{} {}'.format(self.first, self.last)
というmethodが定義されています。そして、このmethodを実行するために、
emp_1.fullname()
emp_2.fullname()
のように、instance名の後ろに、.method名()を付けています。
このように、methodを実行する場合は()を付け、attributeを引き出す場合には()は付けません。
7.classからmethodへのaccess
次のcodeは、先ほどの最後のcodeと同じ結果を生じさせます。
print(Employee.fullname(emp_1)) # same as emp_1.fullname()
このcodeはargumentとしてemp_1をEmployeeに送ります。結局、def fullname(self)におけるselfとは、instance名だということになります。
Part 2へ続きます。
