Python property使用简介
property使用简介
by:授客 QQ:1033553122
功能简介
1) 把类方法变成只读属性
2) setter和getter的另一种实现
代码演示1
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
class User(object):
def __init__(self, username, password):
self._username = username
self._password = password
@property
def username(self):
return self._username
@username.setter
def username(self, username):
self._username = username
@property
def password(self):
return self._password
@password.setter
def password(self, password):
self._password = password
if __name__ == '__main__':
boy = User('shouke', 'shouke2014')
print('对象用户名:', boy.username)
boy.username = 'shou ke'
print('修改后的用户名:', boy.username)
print('通过修改属性值来修改密码')
boy._password = 2014
print('修改后的用户密码:', boy._password)
boy.password = 'shouke2016'
print('通过方法属性来修改密码,修改后的用户密码:', boy._password)
运行结果:
对象用户名: shouke
修改后的用户名: shou ke
通过修改属性值来修改密码
修改后的用户密码: 2014
通过方法属性来修改密码,修改后的用户密码: shouke2016
注意:
1、@property和@function.setter需要成对使用,如下
@property
def function_name
@function.setter
def function_name(self, attribute)
2、如果变量属性值和方法属性值相同,那么以下情况下是无法完成初始化函数 __init__ 中的赋值操作的,即无法初始化对象
代码演示2
#!/usr/bin/env python
#
-*- coding:utf-8 -*-
__author__
=
'shouke'
class
Tester(object):
def
__init__(self,
name, sex, title):
self.name
= name
self.sex
= sex
self.title
= title
@property
def
name(self):
return
self.name
@property
def
sex(self):
return
self.sex
@property
def
title(self):
return
self.title
@title.setter
def
title(self,
title):
self.title
=
title
if
__name__ ==
'__main__':
tester
= Tester('shouke',
'M',
'Tester')
print(tester.title)
"D:\Program
Files\python33\python.exe" E:/Projects/untitled/py1.py
Traceback (most recent call last):
File
"E:/Projects/untitled/py1.py", line 30, in
tester
= Tester('shouke', 'M', 'Tester')
File
"E:/Projects/untitled/py1.py", line 8, in __init__
self.name
= name
AttributeError: can't set attribute
Python property使用简介的更多相关文章
- python property详解
Python中有一个被称为属性函数(property)的小概念,它可以做一些有用的事情.在这篇文章中,我们将看到如何能做以下几点: 将类方法转换为只读属性 重新实现一个属性的setter和getter ...
- python property
python property 在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]] ...
- Python生态环境简介[转]
Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy 原译: Python生态环境简介 当 ...
- python property装饰器
直接上代码: #!/usr/bin/python #encoding=utf-8 """ @property 可以将python定义的函数“当做”属性访问,从而提供更加友 ...
- Python @property 详解
本文讲解了 Python 的 property 特性,即一种符合 Python 哲学地设置 getter 和 setter 的方式. Python 有一个概念叫做 property,它能让你在 Pyt ...
- (转)python生态环境简介
Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy 原译: Python生态环境简介 当 ...
- Python property() 函数
Python property() 函数 Python 内置函数 描述 property() 函数的作用是在新式类中返回属性值. 语法 以下是 property() 方法的语法: class pro ...
- Python 3 mysql 简介安装
Python 3 mysql 简介安装 一.数据库是什么 1. 什么是数据库(DataBase,简称DB) 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据 ...
- python property用法
参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...
随机推荐
- lazy_import源码解析(原创)
参考链接: An approach to lazy importing in Python 3.7(这个是参考源) Python3.7中一种懒加载的方式(中文翻译) 原博客核心: 以前的两种惰性/延迟 ...
- Python+selenium 2【测试报告】
HTML报告 http://tungwaiyip.info/software/HTMLTestRunner.html 下载地址 这个扩展非常简单,只有一个HTMLTestRunner.py文件,选 ...
- [转]迄今为止最优的Eclipse运行性能调优 ,含eclipse.ini
最近,Eclipse(Eclipse-JEE3.5)运行十分缓慢(可能插件安装过多),因此,得到了个机会调优一下,以便提高工作效率 下图是未经任何调整eclipse的gc情况(使用jvisualvm命 ...
- HP-JavaUtil: xls 操作类
Written In The Font 谢谢,陈明.哈哈!共勉,努力搞定它. 路漫漫其修远兮,吾将上下而求索 Content ExportExcelAndSave( String[] header, ...
- Windows 下获取硬盘序列号
只获取序列号 以下任意一条命令都可以: wmic diskdrive get serialnumber wmic path win32_physicalmedia get SerialNumber w ...
- 10分钟学会使用YOLO及Opencv实现目标检测(下)|附源码
将YOLO应用于视频流对象检测 首先打开 yolo_video.py文件并插入以下代码: # import the necessary packages import numpy as np impo ...
- excel 中批量生成mysql的脚本
一.假设你的表格有A.B.C三列数据,希望导入到你的数据库中表格table,对应的字段分别是col1.col2.col3 二.在你的表格中增加一列,利用excel的公式自动生成sql语句,具体方法如下 ...
- shell脚本--echo和printf打印输出
bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 注:本文关于引号等特殊符号的处理仅仅只是几个例子,想要彻底搞明 ...
- Netty 粘包 & 拆包 & 编码 & 解码 & 序列化 介绍
目录: 粘包 & 拆包及解决方案 ByteToMessageDecoder 基于长度编解码器 基于分割符的编解码器 google 的 Protobuf 序列化介绍 其他的 前言 Netty 作 ...
- .NET CORE 实践(2)--对Ubuntu下安装SDK的记录
根据官网Ubuntu安装SDK操作如下: allen@allen-Virtual-Machine:~$ sudo apt-key adv --keyserver apt-mo.trafficmanag ...