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使用简介的更多相关文章

  1. python property详解

    Python中有一个被称为属性函数(property)的小概念,它可以做一些有用的事情.在这篇文章中,我们将看到如何能做以下几点: 将类方法转换为只读属性 重新实现一个属性的setter和getter ...

  2. python property

    python property 在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]] ...

  3. Python生态环境简介[转]

    Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy  原译: Python生态环境简介 当 ...

  4. python property装饰器

    直接上代码: #!/usr/bin/python #encoding=utf-8 """ @property 可以将python定义的函数“当做”属性访问,从而提供更加友 ...

  5. Python @property 详解

    本文讲解了 Python 的 property 特性,即一种符合 Python 哲学地设置 getter 和 setter 的方式. Python 有一个概念叫做 property,它能让你在 Pyt ...

  6. (转)python生态环境简介

    Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy  原译: Python生态环境简介 当 ...

  7. Python property() 函数

    Python property() 函数  Python 内置函数 描述 property() 函数的作用是在新式类中返回属性值. 语法 以下是 property() 方法的语法: class pro ...

  8. Python 3 mysql 简介安装

    Python 3 mysql 简介安装 一.数据库是什么 1.  什么是数据库(DataBase,简称DB) 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据 ...

  9. python property用法

    参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...

随机推荐

  1. ubuntu16.04 下使用vscode备忘录

    微软的vscode是为程序员做了非常大贡献,其强大的功能和各个平台的可移植性给vscode带来了非常大的火力.在程序员的世界中非常的流行,算是一线明星了. 我把使用过程中遇到的一些问题做个记录,方便自 ...

  2. StringBuffer 和 StringBuilder 的 3 个区别

    StringBuffer 和 StringBuilder 它们都是可变的字符串,不过它们之间的区别是 Java 初中级面试出现几率十分高的一道题.这么简单的一道题,栈长在最近的面试过程中,却经常遇到很 ...

  3. linux 根据端口查看系统进程

    1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 注意不同用户下,查看的进程不同

  4. centos7系统配置记录SFTP操作日志

    1.修改ssh配置 [root@elk-node2 ~]# vim /etc/ssh/sshd_config 大概132行把下面这个句注释掉 #Subsystem       sftp    /usr ...

  5. mysql 开发基础系列8 表的存储引擎

    一. 表的存储引擎 1. 概述 插件式存储引擎是mysql数据库最重要的特性之一, 用户可以根据应用的需要选择如何存储和索引数据,是否使用事务等.在mysql 5.0里支持的引擎包括: MyISAM, ...

  6. Chapter 4 Invitations——10

    "Mr. Cullen?" the teacher called, seeking the answer to a question that I hadn't heard. “C ...

  7. Linux 在文件中查找某字符串

    命令: grep 'word' filename 在多个文件中查找: grep 'word' file1 file2 file3 更多用法参考:https://www.howtoforge.com/t ...

  8. Django 系列博客(一)

    Django 系列博客(一) 前言 学习了 python 这么久,终于到了Django 框架.这可以说是 python 名气最大的web 框架了,那么从今天开始会开始从 Django框架的安装到使用一 ...

  9. H5调取APP或跳转至下载

    来源: 最近在配合移动端做几个详情页h5分享页面,需要调取App并跳转至app详情页, 如果没有安装App,需要判断引导至下载页面. 参考文档: https://juejin.im/post/5b7e ...

  10. [转]Ubuntu18.04下使用Docker Registry快速搭建私有镜像仓库

    本文转自:https://blog.csdn.net/BigData_Mining/article/details/88233015 1.背景 在 Docker 中,当我们执行 docker pull ...