<Python> propertyとは、、、

16/4/8 ちと書き直し

プロパティーpropertyとは、どうも次のようなものらしい。

  • アトリビュート attributeと、プロパティー propertyは同じもの。
  • ただ、プロパティーは、propertyデコレータ使って、自分でコントロールできるものが付加できる、、がナイスらしい。

例1

1.. クラス&インスタンス作成

In [5]: class aaa():
   ...:     def __init__(self, x):
   ...:         self._x = x
   ...:     @property
   ...:     def p(self):
   ...:         return None
   ...:     @p.setter
   ...:     def p(self, x):
   ...:         self._x = x
   ...:     @p.getter
   ...:     def p(self):
   ...:         return 'Hi, ' + self._x
   ...:     @p.deleter
   ...:     def p(self):
   ...:         del self._x

In [6]: AAA = aaa('Hage')

2.. getterにアクセス。

In [7]: AAA.p
Out[7]: 'Hi, Hage'

3.. setter

In [8]: AAA.p('HageHage')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-3cd950dacac5> in <module>()
----> 1 AAA.p('HageHage')

TypeError: 'str' object is not callable

4.. エラー、、、

5.. ああ、=か、、

In [9]: AAA.p = 'HageHage'

In [10]: AAA.p
Out[10]: 'Hi, HageHage'

6.. deleter

In [11]: del AAA.p

In [12]: AAA.p
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-1f1d27439d12> in <module>()
----> 1 AAA.p

<ipython-input-5-b0a7f598f238> in p(self)
     10     @p.getter
     11     def p(self):
---> 12         return 'Hi, ' + self._x
     13     @p.deleter
     14     def p(self):

AttributeError: 'aaa' object has no attribute '_x'

ふーん、、、

素のアトリビュートだとこんな感じ、、

In [2]: class aaa2():
   ...:     def __init__(self, x):
   ...:         self._x = x
   ...:         

In [4]: AAA2 = aaa2('Hagezo')

In [5]: AAA2._x
Out[5]: 'Hagezo'

In [6]: AAA2._x = 'Hi, Hagejanaiyo-'

In [7]: AAA2._x
Out[7]: 'Hi, Hagejanaiyo-'

例2

builder.japan.zdnet.com

読み取り専用のプロパティーを見かけ上設定できるっぽい。

In [80]: class bbb():
    ...:     def __init__(self):
    ...:         self.__x = 1
    ...:         self.__y = 2
    ...:         self.a = 1
    ...:         self.b = 2    
    ...:     pass
    ...:     @property
    ...:     def x(self):
    ...:         return self.__x
    ...: 

In [81]: B = bbb()

と作った時、xには、

In [84]: B.x
Out[84]: 1

In [85]: B.x = 1
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-85-d62528f293c5> in <module>()
----> 1 B.x = 1

AttributeError: can't set attribute

となって、読み出しのみ。
対して、aでは、

In [86]: B.a
Out[86]: 1

In [87]: B.a = 2

In [88]: B.a
Out[88]: 2

書き換え可能。
ただ、dir()で見ると、

In [82]: dir(B)
Out[82]: 
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_bbb__x',
 '_bbb__y',
 'a',
 'b',
 'x']

_bbb__xが見えて、

In [89]: B._bbb__x
Out[89]: 1

In [90]: B._bbb__x = 2

In [91]: B._bbb__x
Out[91]: 2

In [92]: B.x
Out[92]: 2

となり、結局書き換え可能だけど、まあ、xyと揃えた時には、
xのみ読み出しOnlyとなる。

ふーん、


例3 スタックさん

stackoverflow.com

これによると、結局、
アトリビュート attributeと、プロパティー propertyは同じもの。
ただ、プロパティーは、propertyデコレータ使って、上記のように、自分でコントロールできるものが付加できる、、がナイスらしい。

ふーん、

マニュアル。

2. 組み込み関数 — Python 3.5.1 ドキュメント