-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate_property.py
More file actions
45 lines (33 loc) · 837 Bytes
/
private_property.py
File metadata and controls
45 lines (33 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# encoding: utf-8
# http://docs.python.org/library/functions.html#property
import unittest
try:
from oktest import test, ok
except ImportError:
import sys
print u"Require oktest framework"
sys.exit(0)
class AClass(object):
def my_setattr(self, x):
self.__x = x + 1
def my_getattr(self):
return self.__x
x = property(my_getattr, my_setattr)
class BClass(object):
def __init__(self, x):
self.__x = x
@property
def x(self):
return self.__x + 1
class Test(unittest.TestCase):
@test("propertyでgetとsetが呼べる")
def _(self):
a = AClass()
a.x = 1
ok(a.x) == 2
@test("@propertyで関数を属性アクセス")
def _(self):
b = BClass(1)
ok(b.x) == 2
if __name__ == "__main__":
unittest.main()