Python 面向对象高级编程——使用@property

1.1   使用@property

输入成绩score时,需对这个参数进行检查。

>>> class Student(object):

...    def get_score(self):

...        return self.__score

...    def set_score(self, value):

...        if not isinstance(value, int):

...             raise ValueError(‘score must beinteger‘)

...        if value < 0 or value > 100:

...             raise ValueError(‘score mustbetween 0 and 100.‘)

...        self.__score = value

...

>>> s = Student()

>>> s.set_score(60)

>>> s.get_score()

60

>>> s.set_score(999)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<stdin>", line 8, in set_score

ValueError: score must between 0 and 100.

有没有既能检查参数,又可以用类似属性这样简单的方式来访问和修改类的变量呢?

Python内置的@property装饰器就是负责把一个方法变成属性。

>>> class Student(object):

...    @property

...    def score(self):     --score可以理解为属性

...        return self.__score     --返回属性

...    @score.setter

...    def score(self, value):   --score可以理解为属性

...        if not isinstance(value, int):

...             raise ValueError(‘Score must be aninteger.‘)

...        if value < 0 or value > 100:

...             raise ValueError(‘Score mustbetween 0 and 100.‘)

...        self.__score = value    --修改属性

...

>>> s = Student()

>>> s.score = 60    --修改属性,这里不再是调用方法,score属性可读可写

>>> s.score

60

>>> s.score = -1

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<stdin>", line 10, in score

ValueError: Score must between 0 and 100.

定义只读属性—只定义getter方法,不定义setter就是只读

>>> class Student(object):

...    @property

...    def birth(self):

...        return self._birth

...    @birth.setter

...    def birth(self, value):

...        self._birth = value    --birth可读可写

...    @property

...    def age(self):

...        return 2016 - self._birth    --age只读

...

>>> s = Student()

>>> s.birth = 1992

>>> s.birth

1992

>>> s.age

24

时间: 2024-08-26 09:14:47

Python 面向对象高级编程——使用@property的相关文章

Python面向对象高级编程:@property--把方法变为属性

为了限制score的范围,可以通过一个set_score()方法来设置成绩,再通过一个get_score()来获取成绩,这样,在set_score()方法里,就可以检查参数: 1 >>> class Student(object): 2 def get_score(self): 3 return self.__score 4 def set_score(self,value): 5 if not isinstance(value,int): 6 raise ValueError('sec

python面向对象高级编程

python中属性和方法的动态绑定  class Student(object):     pass   # 实例化一个对象     s = Student() # 给这个对象绑定一个属性name s.name = 'John'   print(s.name) John   # 定义一个方法 def set_age(self, age):     self.age = age   # 导入模块     from types import MethodType   #给s这个对象绑定一个set_a

Python面向对象高级编程[email&#160;protected]

使用@property 在绑定属性时,如果直接把属性暴露出去,虽然写起来简单,但是没法检查参数,导致可以把成绩随便改: >>> class Student(object): pass >>> s =Student() >>> s.score=999 >>> s.score 999 这显然不符合逻辑,为了限制score的范围,可以通过一个set_score()方法来设置成绩,再通过一个get_score()来获取成绩,这样,在set_s

Python 面向对象高级编程——定制类

1.1   定制类 1.1.1   __str__ >>> class Student(object): ...    def __init__(self, name): ...        self.name = name ... >>> s = Student('daidai') >>> s.name 'daidai' >>> Student('daidai').name 'daidai' >>> print(

Python 面向对象高级编程——使用枚举和元类

1.1   使用枚举 基于Enum类实现的枚举 >>> fromenum import Enum >>> Month = Enum('Month', ('Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) >>> for name, member inMonth.__members__.items(): ...    print(name,

Python面向对象高级编程-_slots_

使用_slots_ 正常情况下,当定义一个class,创建一个class的实例后,可以给实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Student(object): pass 然后,尝试给实例绑定一个属性: >>> s = Student() >>> s.name = 'Michael' >>> print s.name Michael 还可以尝试给实例绑定一个方法: >>>

Python 面向对象高级编程——使用__slots__

1.1   使用__slots__ 1.1.1   类方法的绑定 实例上添加方法 >>> class Student(object): ...    pass ... >>> def set_age(self, age):     #定义函数作为实例方法,注意self参数 ...    self.age = age ... >>> s = Student() >>> fromtypes import MethodType >&g

Python 面向对象高级编程——多重继承

1.1   多重继承 >>> #最大类 ... class Animal(object): ...    pass ... >>> #大类 ... class Mammal(Animal): ...    pass ... >>> class Bird(Animal): ...    pass ... >>> class Runnable(object): ...    def run(self): ...        print(

python基础-面向对象高级编程

实例化对象绑定属性 s.name = 'Michael' # 动态给实例绑定一个属性 类绑定方法---所有实例化对象均可调用Student.set_score = set_score 实例化对象绑定方法---只有该对象可调用from types import MethodType#绑定方法需要使用MethodTypes.set_age = MethodType(set_age, s) # 给实例绑定一个方法 Python内置的@property装饰器就是负责把一个方法变成属性调用的把一个gett