by shigemk2

当面は技術的なことしか書かない

property

2. 組み込み関数 — Python 2.7ja1 documentation

class Parrot1(object):
    def __init__(self):
        self._voltage = 100000

    # voltage() が同じ名前の読み取り専用属性の “getter” になります。
    # doc がもし与えられたならばそれがプロパティ属性のドキュメント文字列になります。
    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage

class Parrot2(object):
    def __init__(self):
        self._voltage = 100000

    def voltage(self):
        """Get the current voltage."""
        return self._voltage

Parrot1 = Parrot1()
print Parrot1.voltage # 100000
Parrot2 = Parrot2()
print Parrot2.voltage() # 100000