
在Python面向对象编程中,@property装饰器是一个强大而灵活的工具,它允许我们将方法调用伪装成属性访问,从而实现更自然、更安全的对象操作。除了基本的getter/setter功能,@property还有许多高级用法可以显著提升代码质量。本文将深入探讨10种高级用法,涵盖从基础封装到元编程的各个层面。
往期阅读>>>
Python 20 个文本分析的库:效率提升 10 倍的秘密武器
Python 自动化管理Jenkins的15个实用脚本,提升效率
App2Docker:如何无需编写Dockerfile也可以创建容器镜像
Python 自动化识别Nginx配置并导出为excel文件,提升Nginx管理效率
场景:当某个属性的值需要基于其他属性计算得出,且计算成本较高时,可以使用@property结合缓存机制。
代码示例:
fromfunctoolsimportlru_cacheclassCircle:def__init__(self, radius):self._radius = radiusself._diameter = None# 缓存直径@propertydefradius(self):returnself._radius@radius.setterdefradius(self, value):self._radius = valueself._diameter = None# 清除缓存@property@lru_cache(maxsize=1)defarea(self):"""计算并缓存圆的面积"""print("计算面积中...")return3.14159*self.radius**2@propertydefdiameter(self):"""惰性计算直径"""ifself._diameterisNone:print("计算直径中...")self._diameter = 2*self.radiusreturnself._diameter# 使用示例circle = Circle(5)print(circle.area) # 第一次计算print(circle.area) # 从缓存读取circle.radius = 10print(circle.area) # 重新计算print(circle.diameter) # 惰性计算
场景:当多个属性之间存在依赖关系,一个属性变化需要自动更新其他相关属性时。
代码示例:
classRectangle:def__init__(self, width, height):self._width = widthself._height = heightself._area = width*heightself._perimeter = 2* (width+height)@propertydefwidth(self):returnself._width@width.setterdefwidth(self, value):self._width = valueself._update_derived_properties()@propertydefheight(self):returnself._height@height.setterdefheight(self, value):self._height = valueself._update_derived_properties()def_update_derived_properties(self):"""更新所有衍生属性"""self._area = self._width*self._heightself._perimeter = 2* (self._width+self._height)@propertydefarea(self):returnself._area@propertydefperimeter(self):returnself._perimeter# 使用示例rect = Rectangle(4, 5)print(f"面积: {rect.area}, 周长: {rect.perimeter}")rect.width = 6print(f"更新后面积: {rect.area}, 周长: {rect.perimeter}")
场景:需要定义类级别的常量或只读属性,防止意外修改。
代码示例:
classConfiguration:def__init__(self):self._api_key = "sk-1234567890abcdef"self._max_retries = 3@propertydefapi_key(self):"""只读API密钥"""return"***"+self._api_key[-4:] # 部分隐藏@propertydefmax_retries(self):"""只读最大重试次数"""returnself._max_retries@propertydefapi_endpoint(self):"""计算得出的只读属性"""returnf"https://api.example.com/v1?key={self._api_key}"@propertydefVERSION(self):"""类常量"""return"1.0.0"# 使用示例config = Configuration()print(f"API端点: {config.api_endpoint}")print(f"版本: {config.VERSION}")# config.api_key = "new_key" # 会报错,因为没有setter
场景:需要在属性访问时自动进行类型转换或格式化输出。
代码示例:
classProduct:def__init__(self, name, price_cents):self.name = nameself._price_cents = price_cents@propertydefprice(self):"""将分转换为元并格式化"""returnf"¥{self._price_cents / 100:.2f}"@price.setterdefprice(self, value):"""接受字符串或数字,统一转换为分"""ifisinstance(value, str):# 移除货币符号和逗号value = value.replace('¥', '').replace(',', '').strip()self._price_cents = int(float(value) *100)elifisinstance(value, (int, float)):self._price_cents = int(value*100)else:raiseTypeError("价格必须是数字或字符串")@propertydefprice_details(self):"""返回详细的价格信息"""yuan = self._price_cents//100cents = self._price_cents%100return {'total_cents': self._price_cents,'yuan': yuan,'cents': cents,'formatted': self.price }# 使用示例product = Product("笔记本电脑", 599900)print(f"价格: {product.price}")product.price = "7999.99"print(f"新价格: {product.price}")print(f"价格详情: {product.price_details}")
场景:需要记录属性的访问和修改历史,用于调试或审计。
代码示例:
importtimefromfunctoolsimportwrapsdeflog_access(func):"""装饰器:记录属性访问"""@wraps(func)defwrapper(self):timestamp = time.strftime("%Y-%m-%d %H:%M:%S")print(f"[{timestamp}] 访问属性: {func.__name__}")returnfunc(self)returnwrapperdeflog_modification(func):"""装饰器:记录属性修改"""@wraps(func)defwrapper(self, value):timestamp = time.strftime("%Y-%m-%d %H:%M:%S")print(f"[{timestamp}] 修改属性: {func.__name__} = {value}")returnfunc(self, value)returnwrapperclassBankAccount:def__init__(self, owner, initial_balance=0):self.owner = ownerself._balance = initial_balanceself._transaction_log = []@property@log_accessdefbalance(self):returnself._balance@balance.setter@log_modificationdefbalance(self, value):old_balance = self._balanceself._balance = valueself._transaction_log.append({'timestamp': time.time(),'old': old_balance,'new': value,'type': 'BALANCE_UPDATE' })@propertydeftransaction_history(self):"""只读交易历史"""returnself._transaction_log.copy()# 使用示例account = BankAccount("张三", 1000)print(f"余额: {account.balance}")account.balance = 1500account.balance = 1200print(f"交易记录: {account.transaction_history}")
场景:需要确保属性值符合特定的业务规则或约束条件。
代码示例:
classUser:def__init__(self, username, email, age):self._username = Noneself._email = Noneself._age = Noneself.username = username# 使用setter进行初始化验证self.email = emailself.age = age@propertydefusername(self):returnself._username@username.setterdefusername(self, value):ifnotvalue:raiseValueError("用户名不能为空")iflen(value) <3:raiseValueError("用户名至少3个字符")iflen(value) >20:raiseValueError("用户名最多20个字符")ifnotvalue.isalnum():raiseValueError("用户名只能包含字母和数字")self._username = value@propertydefemail(self):returnself._email@email.setterdefemail(self, value):ifnotvalue:raiseValueError("邮箱不能为空")if'@'notinvalue:raiseValueError("邮箱格式不正确")self._email = value@propertydefage(self):returnself._age@age.setterdefage(self, value):ifnotisinstance(value, int):raiseTypeError("年龄必须是整数")ifvalue<0:raiseValueError("年龄不能为负数")ifvalue>150:raiseValueError("年龄不能超过150")self._age = value@propertydefis_adult(self):"""计算属性:是否成年"""returnself._age>= 18@propertydefage_group(self):"""计算属性:年龄分组"""ifself._age<13:return"儿童"elifself._age<20:return"青少年"elifself._age<65:return"成人"else:return"长者"# 使用示例try:user = User("john_doe", "john@example.com", 25)print(f"用户名: {user.username}")print(f"是否成年: {user.is_adult}")print(f"年龄分组: {user.age_group}")user.age = 17# 修改年龄print(f"修改后是否成年: {user.is_adult}")exceptValueErrorase:print(f"错误: {e}")
场景:需要为属性提供多个名称,或重构时保持向后兼容性。
代码示例:
classEmployee:def__init__(self, first_name, last_name, salary):self._first_name = first_nameself._last_name = last_nameself._salary = salary# 主要属性@propertydeffirst_name(self):returnself._first_name@first_name.setterdeffirst_name(self, value):self._first_name = value@propertydeflast_name(self):returnself._last_name@last_name.setterdeflast_name(self, value):self._last_name = value# 别名属性(保持向后兼容)@propertydefgiven_name(self):"""first_name的别名"""returnself.first_name@given_name.setterdefgiven_name(self, value):self.first_name = value@propertydeffamily_name(self):"""last_name的别名"""returnself.last_name@family_name.setterdeffamily_name(self, value):self.last_name = value# 计算属性@propertydeffull_name(self):returnf"{self.first_name} {self.last_name}"@full_name.setterdeffull_name(self, value):"""通过全名设置姓和名"""if' 'invalue:first, last = value.split(' ', 1)self.first_name = firstself.last_name = lastelse:self.first_name = valueself.last_name = ""@propertydefannual_salary(self):"""月薪的别名"""returnself._salary*12@annual_salary.setterdefannual_salary(self, value):"""通过年薪设置月薪"""self._salary = value/12@propertydefmonthly_salary(self):returnself._salary@monthly_salary.setterdefmonthly_salary(self, value):self._salary = value# 使用示例emp = Employee("张", "三", 8000)print(f"全名: {emp.full_name}")print(f"月薪: {emp.monthly_salary}")print(f"年薪: {emp.annual_salary}")# 使用别名emp.given_name = "李"emp.family_name = "四"print(f"新全名: {emp.full_name}")# 通过全名设置emp.full_name = "王 五"print(f"姓: {emp.last_name}, 名: {emp.first_name}")
场景:属性对应昂贵的资源(如数据库连接、文件内容),需要延迟加载和正确管理。
代码示例:
importsqlite3importjsonfrompathlibimportPathclassDataManager:def__init__(self, db_path, config_path):self.db_path = db_pathself.config_path = config_pathself._db_connection = Noneself._config_data = Noneself._cache = {}@propertydefdb_connection(self):"""延迟加载数据库连接"""ifself._db_connectionisNone:print("建立数据库连接...")self._db_connection = sqlite3.connect(self.db_path)# 设置连接属性self._db_connection.row_factory = sqlite3.Rowreturnself._db_connection@propertydefconfig(self):"""延迟加载配置文件"""ifself._config_dataisNone:print("加载配置文件...")withopen(self.config_path, 'r', encoding='utf-8') asf:self._config_data = json.load(f)returnself._config_data@propertydefusers(self):"""缓存用户数据"""if'users'notinself._cache:print("查询用户数据...")cursor = self.db_connection.cursor()cursor.execute("SELECT * FROM users")self._cache['users'] = cursor.fetchall()returnself._cache['users']defclear_cache(self, key=None):"""清除缓存"""ifkey:self._cache.pop(key, None)else:self._cache.clear()print("缓存已清除")defclose(self):"""清理资源"""ifself._db_connection:self._db_connection.close()self._db_connection = Noneself._config_data = Noneself._cache.clear()print("资源已清理")# 使用示例manager = DataManager("example.db", "config.json")print(f"配置项: {manager.config.get('app_name')}")print(f"用户数量: {len(manager.users)}")print(f"再次访问用户: {len(manager.users)}") # 从缓存读取manager.clear_cache('users')manager.close()
场景:需要根据用户角色或权限控制属性的访问和修改。
代码示例:
classSecureDocument:def__init__(self, content, owner, security_level=0):self._content = contentself._owner = ownerself._security_level = security_levelself._access_log = []def_check_permission(self, user, required_level):"""检查用户权限"""ifuser.role == 'admin':returnTrueifuser.role == 'owner'anduser.username == self._owner:returnTruereturnuser.clearance_level>= required_leveldef_log_access(self, user, action):"""记录访问日志"""self._access_log.append({'user': user.username,'action': action,'timestamp': time.time() })@propertydefcontent(self):"""受保护的content属性"""# 这里需要传入user对象,实际应用中可能从上下文获取raiseAttributeError("请使用get_content(user)方法")defget_content(self, user):"""安全的内容获取方法"""ifnotself._check_permission(user, self._security_level):raisePermissionError(f"用户 {user.username} 没有权限访问此文档")self._log_access(user, 'READ')returnself._content@propertydefmetadata(self):"""公开的元数据"""return {'owner': self._owner,'security_level': self._security_level,'access_count': len(self._access_log) }@propertydefsummary(self):"""公开的摘要(低安全级别)"""iflen(self._content) >100:returnself._content[:100] +"..."returnself._contentdefset_content(self, user, new_content):"""安全的内容设置方法"""ifuser.username!= self._owneranduser.role!= 'admin':raisePermissionError("只有所有者或管理员可以修改内容")self._log_access(user, 'WRITE')self._content = new_contentclassUser:def__init__(self, username, role='user', clearance_level=0):self.username = usernameself.role = roleself.clearance_level = clearance_level# 使用示例doc = SecureDocument("这是一份机密文档内容...", "admin", security_level=2)user1 = User("alice", role="user", clearance_level=1)user2 = User("admin", role="admin", clearance_level=3)print(f"文档元数据: {doc.metadata}")print(f"文档摘要: {doc.summary}")try:content = doc.get_content(user1)print(f"用户 {user1.username} 获取的内容: {content}")exceptPermissionErrorase:print(f"权限错误: {e}")try:content = doc.get_content(user2)print(f"用户 {user2.username} 获取的内容: {content}")exceptPermissionErrorase:print(f"权限错误: {e}")
场景:需要动态创建具有特定行为的property装饰器,或批量处理类属性。
代码示例:
defvalidated_property(validator_func, error_message=None):"""创建带有验证的property装饰器工厂"""defdecorator(func):@propertydefwrapper(self):returnfunc(self)@wrapper.setterdefwrapper(self, value):ifnotvalidator_func(value):iferror_message:raiseValueError(error_message)else:raiseValueError(f"值 {value} 未通过验证")# 调用原始的setter或直接设置属性ifhasattr(func, '__set__'):func.__set__(self, value)else:# 如果没有setter,直接设置私有属性private_name = '_'+func.__name__setattr(self, private_name, value)returnwrapperreturndecoratordefrange_validator(min_val, max_val):"""范围验证器"""defvalidator(value):returnmin_val<= value<= max_valreturnvalidatordefregex_validator(pattern):"""正则表达式验证器"""importredefvalidator(value):returnbool(re.match(pattern, value))returnvalidatorclassProduct:def__init__(self, name, price, quantity):self.name = nameself._price = priceself._quantity = quantity# 使用装饰器工厂创建验证属性@validated_property(validator_func=range_validator(0, 10000),error_message="价格必须在0-10000之间" )defprice(self):returnself._price@price.setterdefprice(self, value):self._price = value@validated_property(validator_func=lambdax: isinstance(x, int) andx>= 0,error_message="数量必须是非负整数" )defquantity(self):returnself._quantity@quantity.setterdefquantity(self, value):self._quantity = value@propertydeftotal_value(self):returnself.price*self.quantity# 动态为类添加propertydefadd_timestamp_property(cls):"""为类添加时间戳属性"""@propertydeftimestamp(self):importtimereturntime.time()cls.timestamp = timestampreturncls@add_timestamp_propertyclassDynamicClass:def__init__(self, data):self.data = data# 使用示例product = Product("手机", 2999, 10)print(f"产品总价值: {product.total_value}")try:product.price = 15000# 会触发验证错误exceptValueErrorase:print(f"验证错误: {e}")product.quantity = 20print(f"新数量: {product.quantity}, 新总价值: {product.total_value}")dynamic_obj = DynamicClass("测试数据")print(f"动态添加的时间戳: {dynamic_obj.timestamp}")
通过这10种高级用法,我们可以看到@property装饰器在Python中的强大灵活性。从基本的属性封装到复杂的元编程应用,@property都能帮助我们编写更安全、更优雅、更高效的代码。
建议:
适度使用:不要过度使用@property,简单的公共属性直接暴露即可
性能考量:计算密集型属性考虑使用缓存机制
错误处理:在setter中提供清晰的错误信息
保持一致性:类似的属性使用相同的验证和访问模式
文档完善:为每个property添加清晰的文档字符串
掌握这些高级用法,你将能更好地利用Python的面向对象特性,构建更健壮、更易维护的应用程序。
