プロパティ
Pythonでプロパティを定義する場合、property関数とpropertyデコレータを使用する方法があります。
第一引数でgetterのみ指定しているため、setterはありません。つまり読み取り専用のプロパティであるため「url」を更新しようとするとエラーになります。
# -*- coding: utf-8 -*-
class PropertyTest(object):
def __init__(self, url):
self._url = url
def get_url(self):
print '-- get_url --'
return self._url
url = property(get_url)
prop = PropertyTest('http://www.python-izm.com/')
# プロパティ「url」を取得
print prop.url
# getterのみの定義なので更新しようとするとエラー
# prop.url = 'HTTP://WWW.PYTHON-IZM.COM/'
--実行結果--
-- get_url -- http://www.python-izm.com/
property関数はgetter、setter、deleter、ドキュメント文字列を渡すことができます。下記の例ではsetterを定義しているため、プロパティを更新することができます。
# -*- coding: utf-8 -*-
class PropertyTest(object):
def __init__(self, url):
self._url = url
def get_url(self):
print '-- get_url --'
return self._url
def set_url(self, url):
print '-- set_url --'
self._url = url
def del_url(self):
del self._url
url = property(get_url, set_url, del_url, 'url Property')
prop = PropertyTest('http://www.python-izm.com/')
# setter(set_url)にアクセス
prop.url = 'HTTP://WWW.PYTHON-IZM.COM/'
# getter(get_url)にアクセス
print prop.url
--実行結果--
-- set_url -- -- get_url -- HTTP://WWW.PYTHON-IZM.COM/
getterは他の属性を参照しても構いません。次の例は「schema」と「host」からurl文字列を返しています。
# -*- coding: utf-8 -*-
class PropertyTest(object):
def __init__(self, scheme, host):
self.schema = scheme
self.host = host
def get_url(self):
return '{}://{}/'.format(self.schema, self.host)
url = property(get_url)
prop = PropertyTest('http', 'www.python-izm.com')
print prop.url
--実行結果--
http://www.python-izm.com/
関数に「@property」を付与するとその関数はgetterになります。setterは定義していないので、読み取り専用のプロパティです。「url」を更新しようとするとエラーになります。
# -*- coding: utf-8 -*-
class PropertyTest(object):
def __init__(self, url):
self._url = url
@property
def url(self):
print '-- get_url --'
return self._url
prop = PropertyTest('http://www.python-izm.com/')
# プロパティ「url」を取得
print prop.url
# getterのみの定義なので更新しようとするとエラー
# prop.url = 'HTTP://WWW.PYTHON-IZM.COM/'
--実行結果--
-- get_url -- http://www.python-izm.com/
次のような形でgetter、setter、deleterを定義することができます。この例ではsetterを定義しているため、プロパティを更新することができます。
# -*- coding: utf-8 -*-
class PropertyTest(object):
def __init__(self, url):
self._url = url
@property
def url(self):
print '-- get_url --'
return self._url
@url.setter
def url(self, url):
print '-- set_url --'
self._url = url
@url.deleter
def url(self):
del self._url
prop = PropertyTest('http://www.python-izm.com/')
# setterにアクセス
prop.url = 'HTTP://WWW.PYTHON-IZM.COM/'
# getterにアクセス
print prop.url
--実行結果--
-- set_url -- -- get_url -- HTTP://WWW.PYTHON-IZM.COM/
getterは他の属性を参照しても構いません。次の例は「schema」と「host」からurl文字列を返しています。
# -*- coding: utf-8 -*-
class PropertyTest(object):
def __init__(self, scheme, host):
self.schema = scheme
self.host = host
@property
def url(self):
return '{}://{}/'.format(self.schema, self.host)
prop = PropertyTest('http', 'www.python-izm.com')
print prop.url
--実行結果--
http://www.python-izm.com/
旧スタイルのクラスでプロパティを定義すると、一見して問題なく動作しているように見えます。ただし下記例のようにgetterしか定義していない読み取り専用のプロパティであるにも関わらず「url」が更新できてしまいます。プロパティを定義する時は新スタイルでクラスを作成しましょう。
※旧クラススタイル、新クラススタイルの詳細は新旧クラススタイルを参照してください。
# -*- coding: utf-8 -*-
class PropertyTest:
def __init__(self, url):
self._url = url
@property
def url(self):
print '-- get_url --'
return self._url
prop = PropertyTest('http://www.python-izm.com/')
print prop.url
prop.url = 'HTTP://WWW.PYTHON-IZM.COM/'
print prop.url
--実行結果--
-- get_url -- http://www.python-izm.com/ HTTP://WWW.PYTHON-IZM.COM/
次は便利な設定ファイル!
