# Python learning
# coding:utf-8
# 装饰器是在不修改被装饰对象的源代码以及调用方式的前提下为被装饰对象添加新功能的可调用对象
# property是一个装饰器, 是用来将绑定给对象的方法伪装成一个数据属性
print(property)
# 案例1
# class People:
# def __init__(self, name, height, weight):
# self.name = name
# self.height = height
# self.weight = weight
#
# # 定义函数的原因:
# # 1、bmi应该是触发功能计算得到的
# # 2、bmi是随着身高、体重变化而动态变化的
#
# # 但bmi(体质指数)听起来更像一个数据属性,而非功能
# @property
# def bmi(self):
# return self.weight / (self.height ** 2)
#
#
# obj1 = People('sepia', 1.8, 66)
# res = obj1.bmi
# print(res)
# 案例2
# class People:
# def __init__(self, name):
# self.__name = name
#
# def get_name(self):
# return self.__name
#
# def set_name(self, val):
# if type(val) is not str:
# print('必须传入str类型')
# return
# self.__name = val
#
# def del_name(self):
# print('不让删除')
# # del self.__name
#
# name = property(get_name, set_name, del_name)
# 案例3(推荐)
class People:
def __init__(self, name):
self.__name = name
@property # name = property(name)
def name(self):
return self.__name
@name.setter
def name(self, val):
if type(val) is not str:
print('必须传入str类型')
return
self.__name = val
@name.deleter
def name(self):
print('不让删除')
# del self.__name
obj1 = People('sepia')
# 案例1
# print(obj1.get_name)
# obj1.set_name('egon')
# print(obj1.get_name)
# obj1.del_name
# 案例2,3
print(obj1.name)
obj1.name = 'egon'
print(obj1.name)
del obj1.name