看不懂,先记录
一、
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Student(object):
@property
def score(self):
return self._score
@score.setter #setter是哪里来的??作用呢???
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value
s = Student()
s.score = 60
print s.score
运行结果:60
二、
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Student1(object):
@property #????
def birth(self):
return self._birth
@birth.setter #????
def birth(self, value):
self._birth = value
@property #????
def age(self):
return 2014 - self._birth
s = Student1()
s.birth = 60
print s.age
运行结果:60
原文地址:http://blog.51cto.com/13502993/2147278