8
8

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

propertyを使うときはobjectを継承したクラス (new-style class) を使う

8
Last updated at Posted at 2013-02-17

new-style classじゃない場合##

class Hoge:
    def __init__(self):
        self._x = 1

    @property
    def x(self):
        print 'property', self._x
        return self._x

    @x.setter
    def x(self, x):
        print 'setter'
        self._x = x

a = Hoge()
print a.x

a.x = 2
print a.x

結果##

property 1
1
2

new-style classの場合##

class Hoge(object):
    def __init__(self):
        self._x = 1

    @property
    def x(self):
        print 'property', self._x
        return self._x

    @x.setter
    def x(self, x):
        print 'setter'
        self._x = x

a = Hoge()
print a.x

a.x = 2
print a.x

結果##

property 1
1
setter
property 2
2

参考##

8
8
0

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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?