python中property干什么用的?

先来段官方文档压压惊。。
property(fget=None, fset=None, fdel=None, doc=None)

Return a property attribute.

fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del’ing, an attribute. Typical use is to define a managed attribute x:

class C:
    def __init__(self):
        self._x = None

    def getx(self):
        return self._x
    def setx(self, value):
        self._x = value
    def delx(self):
        del self._x
    x = property(getx, setx, delx, "I‘m the ‘x‘ property.")

If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.


property的用法是为你的类设置一个属性值。第一个参数设定获取属性值的方法,第二个参数设定设置这个属性值的方法,第三个参数设定删除这个属性值的方法,第四个参数是文档。c.x会调用第一个参数的方法,c.x = value 调用第二个方法,del c.x调用第三个方法。这样

时间: 2024-08-02 00:23:12

python中property干什么用的?的相关文章

python中@property的使用

python中@property的使用 @property的作用是把一个getter方法变成属性,@xxx.setter把setter方法变成属性赋值. #coding:utf-8 class Screen(object): @property def width(self): return self._width @width.setter def width(self,value): if not isinstance(value,int): raise ValueError('width

python中property的使用

记得在cocoa中也有property的概念,python中的property与cocoa里的property好像是差不多的.下面是它的两种用法. 用法一: class test(object): def __init__(self): print "init is calling" self.a=100 def getX(self): print "call getX" return self.a def setX(self,value): print "

python中@property的作用和getter setter的解释

@property作用: python的@property是python的一种装饰器,是用来修饰方法的. 我们可以使用@property装饰器来创建只读属性,@property装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用,这样可以防止属性被修改. 1.修饰方法,让方法可以像属性一样访问. class DataSet(object): @property def method_with_property(self): ##含有@property return 15 def m

Python中property属性详解

1. 什么是property属性 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法 # ############### 定义 ############### class Foo: def func(self): pass # 定义property属性 @property def prop(self): pass # ############### 调用 ############### foo_obj = Foo() foo_obj.func() # 调用实例方法 foo_obj.p

python中 @property与

考察 Student 类: class Student(object): def __init__(self, name, score): self.name = name self.score = score 当我们想要修改一个 Student 的 scroe 属性时,可以这么写: s = Student('Bob', 59) s.score = 60 但是也可以这么写: s.score = 1000 显然,直接给属性赋值无法检查分数的有效性. 如果利用两个方法: class Student(

python 中@property的使用

从14年下半年开始接触到python,自学了一段时间,后又跟别人学习了下,把基础知识基本上学过了.忽然感觉python不可能这么简单吧,就这么点东西?后来看了下书,发现还有很多的高级部分.连续看了两天,把装饰符@看了下,记录下. @装饰符的作用就是类里的方法变成属性使用,比直接调用方法要直接简单 直接上代码(没有@的): 1 class Student(object): 2 def get_age(self): 3 return self.__age 4 def set_age(self,age

Python中@property和@classmethod和@staticmethod

前戏 首先,先要弄清楚一个类里面的,各个组成部分都应该怎么称呼. - 注:可能叫法会不太一样. 关于@property 顾名思义:它的意思为'属性'. 作用: 1:使用它你将会把类方法,变为类属性.并且是只读属性. 2:它会重新实现getter和setter方法. 看代码: class Person: def __init__(self,first_name,last_name): self.first_name = first_name self.last_name = last_name @

python中property的简单应用场景

多注意看最后的两个print,一个是name,一个是name2当然可以再增加个name3 原文地址:http://blog.51cto.com/13750690/2296187

Python中的@property和decorator

初次认识decorator和@property Welcome. 在本文中,将详细学习如何使用Python中的decorator和@property. 将会学习的内容: 使用decorator的优势. 使用@property的优势. 装饰器函数的基础知识:它们是什么以及如何与@property关联起来. 如何使用@property定义getter.setter和deleter. Python中decorator的优势 在不确定其他代码块对现有函数的调用,不改变现有函数,保证代码可靠稳定,避免修改