0
1

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 3 years have passed since last update.

クラス変数、クラスメソッドとスタティスティックメソッド

Last updated at Posted at 2020-08-25
qiita.py
class Person(object):

    kind = 'human'

    def __init__(self,name):
        self.name = name

    def who_are_you(self):
        print(self.name,self.kind)

a = Person('A')
a.who_are_you()
# A human

b = Person('B')
b.who_are_you()
# B human

注意点

クラス変数は全てのオブジェクトで共有される

qiita.py
class T(object):

    words = []

    def add_word(self,word):
        self.words.append(word)
c = T()
c.add_word('add1')
c.add_word('add2')

d = T()
d.add_word('add3')
d.add_word('add4')
print(c.words)
# ['add1', 'add2', 'add3', 'add4']

このように書き換える必要がある
initで初期化する

qiita.py
class T(object):
    def __init__(self):
        self.words = []

    def add_word(self,word):
        self.words.append(word)
c = T()
c.add_word('add1')
c.add_word('add2')
print(c.words)
# ['add1', 'add2']

d = T()
d.add_word('add3')
d.add_word('add4')
print(d.words)
# ['add3', 'add4']

クラスメソッド

qiita.py
class Person(object):

    kind = 'human'

    def __init__(self):
        self.x = 100

a = Person()
print(a)
b = Person
print(b)

実行すると
aでは『オブジェクトだと表示』され、
bでは『クラスのみを示し、オブジェクト化されていない』と表示される。

<__main__.Person object at 0x7fc0558d6210>
<class '__main__.Person'>

self.xにアクセスしようとすると
aでは100と表示されるが
bではinitが実行されていない為エラーが発生する

qiita.py
class Person(object):

    kind = 'human'

    def __init__(self):
        self.x = 100

a = Person()
print(a.x)
b = Person
print(b.x)
100
Traceback (most recent call last):
  File "/Users/kirinboy96/PycharmProjects/untitled1/lesson_package/NEW1.py", line 11, in <module>
    print(b.x)
AttributeError: type object 'Person'

ただ、この場合でもkindには$\color{red}{\rm アクセス可能}$

qiita.py
class Person(object):

    kind = 'human'

    def __init__(self):
        self.x = 100

a = Person()
print(a.kind)
# 実行結果:human
b = Person
print(b.kind)
# 実行結果:human

クラスメソッド

オブジェクト化されていなくても、アクセスする方法としてクラスメソッドが存在する

qiita.py
class Person(object):

    kind = 'human'

    def __init__(self):
        self.x = 100
        
    @classmethod
    def what_is_your_kind(cls):
        return cls.kind

a = Person()
print(a.what_is_your_kind())
# 実行結果:human
b = Person
print(b.what_is_your_kind())
# 実行結果:human

スタティックメソッド

スタティックメソッドにはcls,selfなど使わなくてもOK

qiita.py
class Person(object):

    kind = 'human'

    def __init__(self):
        self.x = 100

    @classmethod
    def what_is_your_kind(cls):
        return cls.kind

    @staticmethod
    def about():
        print("about human")

print(Person.about())
# 実行結果:about human

引数をとる事もできる

qiita.py
class Person(object):

    kind = 'human'

    def __init__(self):
        self.x = 100

    @classmethod
    def what_is_your_kind(cls):
        return cls.kind

    @staticmethod
    def about(year):
        print("about human {}".format(year))

print(Person.about(2020))
# 実行結果:about human 2020
0
1
2

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?