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
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
from types import *
class MyIntProperty(object):
def __init__(self, initval=None):
self.val = 0
def __get__(self, obj, objtype):
return self.val
def __set__(self, obj, val):
self.val = self._isint(val)
def _isint(self, val):
if type(val) is not IntType:
raise Exception('Parameter must be Int')
else:
return val
class MyClass(object):
x = MyIntProperty(10)
y = MyIntProperty()
test = MyClass()
test.x, test.y = 10, 100
print test.x, test.y
test.x, test.y = 20, 'aa'
print test.x, test.y |
Partager