pythonに慣れよう9 クラス

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Yamanote:
    pass
 
shinagawa = Yamanote()
shinagawa.city = "Minato-ku"
shinagawa.user = 370000
shinagawa.line = 24
 
shibuya = Yamanote()
shibuya.city = "Shibuya-ku"
shibuya.user = 3310000
shibuya.spot = "hachiko"
 
print(shinagawa.city)
print(shibuya.spot)

[vagrant@localhost python]$ python app.py
Minato-ku
hachiko

コンストラクタを使う。

1
2
3
4
5
6
7
8
9
class Yamanote:
    def __init__(self, city):
        self.city = city
 
shinagawa = Yamanote("Shinagawa-ku")
shibuya = Yamanote("Shibuya-ku")
 
print(shinagawa.city)
print(shibuya.city)

[vagrant@localhost python]$ python app.py
Shinagawa-ku
Shibuya-ku

クラス変数を呼び出す。

1
2
3
4
5
6
7
8
9
class Yamanote:
    count = 0
    def __init__(self, city):
        Yamanote.count += 1
        self.city = city
 
shinagawa = Yamanote("Shinagawa-ku")
shibuya = Yamanote("Shibuya-ku")
print(Yamanote.count)

ふむ。
[vagrant@localhost python]$ python app.py
2

メソッド

1
2
3
4
5
6
7
8
9
10
11
12
13
class Yamanote:
    count = 0
    def __init__(self, city):
        Yamanote.count += 1
        self.city = city
    def announce(self):
        print("This is " + self.city)
 
shinagawa = Yamanote("Shinagawa-ku")
shibuya = Yamanote("Shibuya-ku")
 
shinagawa.announce()
shibuya.announce()

わかるんだが、使っていかないと、慣れないね。
[vagrant@localhost python]$ python app.py
This is Shinagawa-ku
This is Shibuya-ku

@classmethod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Yamanote:
    count = 0
    def __init__(self, city):
        Yamanote.count += 1
        self.city = city
    def announce(self):
        print("This is " + self.city)
    @classmethod
    def show_info(cls):
        print(str(cls.count) + "instances")
 
shinagawa = Yamanote("Shinagawa-ku")
shibuya = Yamanote("Shibuya-ku")
 
Yamanote.show_info()

あああああ
[vagrant@localhost python]$ python app.py
2instances

class のprivate, public

1
2
3
4
5
6
7
8
9
10
class Yamanote:
    def __init__(self, city):
        self.__city = city
    def announce(self):
        print("This is " + self.__city)
 
shinagawa = Yamanote("Shinagawa-ku")
shibuya = Yamanote("Shibuya-ku")
 
print(shinagawa.__city)

[vagrant@localhost python]$ python app.py
Traceback (most recent call last):
File “app.py”, line 12, in
print(shinagawa.__city)
AttributeError: Yamanote instance has no attribute ‘__city’
ほえ~

継承のsuperがうまくいかない。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Yamanote:
    def __init__(self, spot):
        self.spot = spot
    def announce(self):
        print("Enjoy " + self.spot)
 
class Startup(Yamanote):
    def __init__(self, spot, company):
        super().__init__(spot)
        self.company = company
    def hello(self):
        print("What's up " + self.company)
 
harajyuku = Startup("takeshita","sm")
print(Harajyuku.spot)
Harajyuku.hello()

[vagrant@localhost python]$ python app.py
Traceback (most recent call last):
File “app.py”, line 16, in
harajyuku = Startup(“takeshita”,”sm”)
File “app.py”, line 11, in __init__
super().__init__(spot)
TypeError: super() takes at least 1 argument (0 given)
あ、python2系はエラーになるのね。。早くいってよ、もー

1
2
import math, random
print(math.pi)