<jupyter_start><jupyter_text>Python property Python的property功能是对普通属性的一个增强，并不是必须功能。  修饰器使用`@property`修饰器作为于方法，这样就建立了一个于方法名同名的属性。可以使用`对象.方法名`来获得该方法的返回值，不需要使用括号。使用修饰器可以达到在不修改调用代码的情况下对类的属性行为进行修改。<jupyter_code>class Person:
    def __init__(self, age):
        self._age = age
    
    @property
    def age(self):
        """
        This is age
        """
        return self._age
    
    @age.setter
    def age(self, age):
        if 0 <= age <= 150:
            self._age = age
        else:
            raise ValueError('Age must between 0 and 150')
        
    @age.deleter
    def age(self):
        del self._age<jupyter_output><empty_output><jupyter_text>1. 使用`@property`定义对属性的读取操作是必须的，另外两个属性是可选的。2. 使用`@属性名.setter`定义对属性的写操作。3. 使用`@属性名.deleter`定义对属性的删除操作。4. 如果有定义属性的docstring，在属性的读取操作上定义。5. 三个方法的名字都是一样的，为属性的名字。<jupyter_code>person = Person(50)<jupyter_output><empty_output><jupyter_text>获取属性<jupyter_code>person.age<jupyter_output><empty_output><jupyter_text>设置属性<jupyter_code>try:
    person.age = 200
except ValueError as e:
    print(e)<jupyter_output>Age must between 0 and 150<jupyter_text>删除属性<jupyter_code>del person.age
try:
    person.age
except AttributeError as e:
    print(e)<jupyter_output>'Person' object has no attribute '_age'<jupyter_text>属性函数使用property函数可以达到与使用修改器一样的效果。在原有已经有setter/getter函数的时候，使用property函数定义属性将对定义代码修改影响最小。<jupyter_code>class Person2:
    def __init__(self, age):
        self._age = age
    
    def get_age(self):
        return self._age
    
    def set_age(self, age):
        if 0 <= age <= 150:
            self._age = age
        else:
            raise ValueError('Age must between 0 and 150')
        
    def del_age(self):
        del self._age
    
    age = property(get_age, set_age, del_age, "This is age")<jupyter_output><empty_output><jupyter_text>假设原来已经有`get_age`、`set_age`、`del_age`函数，则只需在最后加一句代码就可以创建属性age。<jupyter_code>person = Person2(50)<jupyter_output><empty_output><jupyter_text>获取属性<jupyter_code>person.age<jupyter_output><empty_output><jupyter_text>设置属性<jupyter_code>person.age = 25
person.age<jupyter_output><empty_output><jupyter_text>删除属性<jupyter_code>del person.age
try:
    person.age
except AttributeError as e:
    print(e)<jupyter_output>'Person2' object has no attribute '_age'
{'fraction_non_alphanumeric': 0.1084070796460177, 'fraction_numerical': 0.016371681415929203, 'mean_word_length': 4.173913043478261, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '290896', 'license': 'MIT', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1094, 'n_words': 126, 'path': 'python/python_property.ipynb', 'repo_name': 'icescut/my.summary', 'size': '2260'}