Python Class: Object-oriented advanced programming @property

Source: Internet
Author: User

I can't read it, I'll record it.


One

#!/usr/bin/env Python3
#-*-Coding:utf-8-*-


Class Student (object):

@property
DEF score (self):
Return Self._score

@score. Setter #setter是哪里来的?? Role???
DEF score (self, value):
If not isinstance (value, int):
Raise ValueError (' score must is 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


Run Result: 60


Two

#!/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


Run Result: 60


Python Class: Object-oriented advanced programming @property

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.