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 "call setX"
        self.a=value

    x=property(getX,setX)

def main():
    a = test()
    print a.x  #100
    a.x=150
    print a.x  #150

if __name__ == ‘__main__‘:
    main()

getX和setX这两个函数名字可以随便取的。还有一个del方法,不太常用,所以这里就不写了

用法二:

class test(object):

    def __init__(self):
        print "init is calling"
        self.a=100

    @property
    def x(self):
        print "call getX"
        return self.a

    @x.setter
    def x(self,value):
        print "call setX"
        self.a=value

def main():
    t = test()
    print t.x  #100
    t.x=150
    print t.x  #150

if __name__ == ‘__main__‘:
    main()

注意到函数名都改成了x。

时间: 2024-08-25 23:11:24

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的作用和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干什么用的?

先来段官方文档压压惊.. 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

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的优势 在不确定其他代码块对现有函数的调用,不改变现有函数,保证代码可靠稳定,避免修改