广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python3 property属性
  • 631
分享到

Python3 property属性

属性property 2023-01-31 08:01:28 631人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

python3中的property有一个很有意思的功能,它能将类中的方法像类属性一样调用!class property(fget=None, fset=None, fdel=None, doc=None)我们先来简单了解一下这个proper

python3中的property有一个很有意思的功能,它能将类中的方法像类属性一样调用!

class property(fget=None, fset=None, fdel=None, doc=None)

我们先来简单了解一下这个property类,下面看一下官网给出的例子:

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.")  # 这里的x相当于类属性
    
c = C()  # 生成一个对象
c.x = 10  # 设置self._x=10,实际调用的就是类中setx方法
c.x  # 获取self._x的值,实际调用的就是类中getx方法
del c.x  # 删除self._x的值,实际调用的就是类中delx方法

    是不是感觉很有意思,很不可思议!property中fget是一个函数,它获取属性值;fset是一个函数,它设置一个属性值;fdel是一个函数,它删除一个属性值;doc为该属性创建一个docstring。你只要在使用时在对应的形参位置放上你写的对应函数,就可以轻松使用了。


我们还可以将property作为装饰器来使用,还是使用官网的例子:

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

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

    property对象有getter、setter、deleter三个方法,getter获取属性值,setter设置属性值,deleter设置属性值,这个例子的效果跟上一个例子的效果完全相同!我们看到里面的方法名是一模一样的,但是达到的效果却是不同的。第一个x方法是获取属性值,第二个x方法是设置属性值,第三个x方法是删除属性值。

    你看到这里是不是以为这一切都是property帮你做到的,错,错,错!其实property只做了一件事件,它将你的方法能像类属性一样使用,至于里面的查、删、改,其实都是你自己写的函数实现的!fget、fset、fdel、setter、deleter这些仅仅只是名字而且,方便你识别,其他什么作用都没有!

我们来看一下property的源代码:

class property(object):
    """
    property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
    
    fget is a function to be used for getting an attribute value, and 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(object):
        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.")
    
    Decorators make defining new properties or modifying existing ones easy:
    
    class C(object):
        @property
        def x(self):
            "I am the 'x' property."
            return self._x
        @x.setter
        def x(self, value):
            self._x = value
        @x.deleter
        def x(self):
            del self._x
    """
    def deleter(self, *args, **kwargs): # real signature unknown
        """ Descriptor to change the deleter on a property. """
        pass

    def getter(self, *args, **kwargs): # real signature unknown
        """ Descriptor to change the getter on a property. """
        pass

    def setter(self, *args, **kwargs): # real signature unknown
        """ Descriptor to change the setter on a property. """
        pass

    def __delete__(self, *args, **kwargs): # real signature unknown
        """ Delete an attribute of instance. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __get__(self, *args, **kwargs): # real signature unknown
        """ Return an attribute of instance, which is of type owner. """
        pass

    def __init__(self, fget=None, fset=None, fdel=None, doc=None): # known special case of property.__init__
        """
        property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
        
        fget is a function to be used for getting an attribute value, and 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(object):
            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.")
        
        Decorators make defining new properties or modifying existing ones easy:
        
        class C(object):
            @property
            def x(self):
                "I am the 'x' property."
                return self._x
            @x.setter
            def x(self, value):
                self._x = value
            @x.deleter
            def x(self):
                del self._x
        
        # (copied from class doc)
        """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __set__(self, *args, **kwargs): # real signature unknown
        """ Set an attribute of instance to value. """
        pass

    fdel = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    fget = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    fset = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    __isabstractmethod__ = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    看到上面的源代码恍然大悟没,fdel、fget、fset都只是执行你函数里面的代码而已!所以我们就记住一句话就够了:“property能让你的方法像类属性一样使用”。



--结束END--

本文标题: Python3 property属性

本文链接: https://www.lsjlt.com/news/193029.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python3 property属性
    python3中的property有一个很有意思的功能,它能将类中的方法像类属性一样调用!class property(fget=None, fset=None, fdel=None, doc=None)我们先来简单了解一下这个proper...
    99+
    2023-01-31
    属性 property
  • python中@Property属性使用方法
    目录一、前言二、创建用于计算的属性三、为属性添加安全保护机制一、前言 本文介绍的属性与类属性和实例属性不同。类属性和实例属性介绍的属性将返回所存储的值。而本文要介绍的属性是一种特殊的...
    99+
    2022-11-11
  • python中@Property属性如何使用
    这篇文章主要介绍“python中@Property属性如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python中@Property属性如何使用”文章能帮助大家解决问题。一、前言本文介绍的属...
    99+
    2023-07-02
  • Python中property属性的用处详解
    目录前言限制值使用 @property 的方式代替。动态属性的好处动态显示附:用property代替getter和setter方法总结前言 Python 动态属性的概念可能会被面试问...
    99+
    2022-11-10
  • css中的transition-property属性怎么用
    小编给大家分享一下css中的transition-property属性怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!  ...
    99+
    2022-10-19
  • Mybatis中resultMap的Colum和property属性详解
    目录resultMap的Colum和property属性1: resultMap标签2:使用情况2.1 简单查询2.2 一对一2.3 一对多resultMap对column和prop...
    99+
    2022-11-12
  • Python中property标签属性怎么使用
    在Python中,可以使用@property装饰器来定义一个属性的getter方法,并使用@property.setter装饰器来定...
    99+
    2023-09-16
    Python
  • Python中property属性的作用是什么
    本篇内容主要讲解“Python中property属性的作用是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python中property属性的作用是什么”吧!前言Python 动态属性的概念...
    99+
    2023-06-30
  • Python3 类属性、类变量
      # -*- coding:utf-8 -*- # 类属性、类变量:只能由类调用的属性 class People(object): # 类变量可以由所有的对象访问,但是对象只能访问,不可修改 # 用来做资源共享...
    99+
    2023-01-31
    变量 类属
  • python3--面向对象的三大特性:封装,property,classmethod,staticmethod
    python中的封装隐藏对象的属性和实现细节,仅对外提供公共访问方式好处:1 将变化隔离2 便于使用3 提供复用性4 提高安全性封装原则1 将不需要对外提供的内容都隐藏起来2 把属性都隐藏,提供公共方法对其访问私有变量和私有方法在pytho...
    99+
    2023-01-30
    三大 面向对象 特性
  • python中的property及属性与特性之间的优先权
    目录 前言属性(attribute)属性的定义属性的用法特性(property)特性的定义特性的用法特性的使用场景属性和特性之间的差别和联系属性和特性之间的优先权 ...
    99+
    2022-11-11
  • Python3 查看 com 组件的属性
    环境 Windows 10 Python 3.6.3 pywin32地址 sourceforge:https://sourceforge.net/projects/pywin32/files/pywin32/ GitHub:http...
    99+
    2023-01-31
    组件 属性
  • python特性--property
    在定义一个类的时候,有时我们需要获取一个类的属性值,而这个属性值需要经过类中的其他属性运算来获得的。那么很容易,只要我们在类中定义一个方法,并且通过调用方法可以获取到那个需要运算的属性值。那么,问题来了,当有一天需求变了,你需要反向操作你...
    99+
    2023-01-30
    特性 python property
  • Android中Property Animation属性动画编写的实例教程
    1、概述 Android提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。View Anima...
    99+
    2022-06-06
    animation 教程 动画 Android
  • Python 使用@property对属性进行数据规范性校验的实现
    在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合...
    99+
    2022-11-12
  • Spring配置文件中property属性的name出错怎么解决
    要解决Spring配置文件中property属性的name出错问题,可以按照以下步骤进行处理:1. 检查错误的name属性是否正确拼...
    99+
    2023-08-14
    Spring property
  • PHP8中如何使用Constructor Property Promotion来简化类的属性声明?
    PHP8是PHP编程语言的最新版本,引入了一项强大的特性,即Constructor Property Promotion(构造函数属性提升)。这个特性使得在类的构造函数中定义和初始化属性变得非常简单和优雅。本文将详细介绍Constructo...
    99+
    2023-10-22
    PHP Constructor Property Promotion 类属性声明
  • python3默认排序函数的多属性比较
    Python3开始sorted函数和list.sort函数不再接收cmp作为参数,只使用key参数作为比较关键词,这样处理多属性的比较就比较麻烦。 一种有效的解决方案是key参数传入比较函数,返回值是所需比较的多个属性按优先级排列的一...
    99+
    2023-01-31
    函数 属性
  • python3中类的重点与难点:类属性和实例属性的区别说明
    先看图理解: 类属性就相当与全局变量,实例对象共有的属性,实例对象的属性为实例对象自己私有。 类属性就是类对象(Tool)所拥有的属性,它被所有类对象的实例对象(实例方法)所共有,...
    99+
    2022-11-12
  • 解决Spring配置文件中bean的property属性中的name出错问题
    Spring配置文件中bean的property属性中的name有错,红色 原因: 在实现类中没有写set方法 解决: 理解Spring配置文件中的property标签中的属性 ...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作