当前位置:网站首页>装饰器《二》 property - 简答逻辑
装饰器《二》 property - 简答逻辑
2022-06-22 03:12:00 【风华浪浪】
一、基础用法
property 装饰方法的行为可以当做属性来用, 可单纯返回一些数据
通过 setter 方法, 属性name 的值可以被改变, 并且在方法里做一些简单的校验
二、 应用示例
class User(object):
def __init__(self, username):
self._username = username
@property
def username(self):
return self._username
@username.setter
def username(self, username):
if len(username) < 5:
raise Exception('name laste len 5')
self._username = username
@property
def user_count(self): # 单纯获取数据库用户的纪录有多少
return self.username.order_by('create_at').count()
if __name__ == '__main__':
user = User('zhangsan')
name = user.username # 输出 zhangsan
# user.username = 'lisi' # username 的属性得到约束 zhangsan
user.username = 'wanger'
name = user.username
print(user.user_count)
三、复杂用法
边栏推荐
- CMD view the console output of hearts, diamonds, spades and clubs to solve the garbled code
- On map state mapping
- AtCoder Regular Contest 142
- [nvme2.0b 8] nvme queue arbitration mechanism
- Lectures explanation for unsupervised graph level representation learning (usib)
- 图数据平台解决方案:单节点部署
- AtCoder Regular Contest 142
- 【NVMe2.0b 6】NVMe 队列模型
- [kubernetes series] what is kubernetes?
- 【论文】低光图像增强的零参考深度曲线估计
猜你喜欢
![[nvme2.0b 5] sous - système nvm](/img/4f/e60e62a04e617b2e7858494917f390.png)
[nvme2.0b 5] sous - système nvm

CMD view the console output of hearts, diamonds, spades and clubs to solve the garbled code

Project management software development project management
![[crawler notes 2] mouse events, screenshots and common attack methods](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[crawler notes 2] mouse events, screenshots and common attack methods

工厂模式

Project Management-软件开发之项目管理

Kubernetes介绍和使用

linux下安装mysql8及使用(转载)

Memory hierarchy introduction

策略模式
随机推荐
torch.max()使用讲解
Overview of web framework and program development
BOM 属性、方法、事件应用案例
Adaptive batch job scheduler: automatically derive parallelism for Flink batch jobs
Force buckle 141 Circular linked list
Force buckle 239 Sliding window Max
What is an SSL certificate and what are the benefits of having an SSL certificate?
Usage of tail
Conference chat room - development documents
[kubernetes series] what is kubernetes?
[nvme2.0b 5] sous - système nvm
NXP imx8mp学习记录
Mysql 字段类型以及对应的长度 & 字节
Sword finger offer 58 Symmetric binary tree
[nvme2.0b 10] controller shutdown and NVM subsystem shutdown
VS正在加载符号导致程序启动变慢
tag动态规划-刷题预备知识-1.动态规划五部曲解题法 + lt.509. 斐波那契数/ 剑指Offer 10.I + lt.70. 爬楼梯彻底解惑 + 面试真题扩展
linux下安装mysql8及使用(转载)
不规范的命名
Modèle stratégique