首页 > 其他分享 >@property_1

@property_1

时间:2023-12-07 10:55:06浏览次数:21  
标签:self labels ._ images property 属性

class DataSet(object):
    def __init__(self):
        self._images = 1
        self._labels = 2 #定义属性的名称
    @property
    def images(self): #方法加入@property后,这个方法相当于一个属性,这个属性可以让用户进行使用,而且用户有没办法随意修改。
        return self._images 
    @property
    def labels(self):
        return self._labels
l = DataSet()
#用户进行属性调用的时候,直接调用images即可,而不用知道属性名_images,因此用户无法更改属性,从而保护了类的属性。
print(l.images) # 加了@property后,可以用调用属性的形式来调用方法,后面不需要加()。

由于python进行属性的定义时,没办法设置私有属性,因此要通过@property的方法来进行设置。这样可以隐藏属性名,让用户进行使用的时候无法随意修改。

标签:self,labels,._,images,property,属性
From: https://www.cnblogs.com/MoKinLi/p/17881225.html

相关文章

  • Data is Null. This method or property cannot be called on Null values.
    升级到abp.io7.4EF报错System.Data.SqlTypes.SqlNullValueException:DataisNull.ThismethodorpropertycannotbecalledonNullvalues.atMicrosoft.Data.SqlClient.SqlBuffer.ThrowIfNull()atMicrosoft.Data.SqlClient.SqlBuffer.get_String()atMicroso......
  • HTMLElement: offsetParent property
    HTMLElement:offsetParentpropertyTheHTMLElement.offsetParentread-onlypropertyreturnsareferencetotheelementwhichistheclosest(nearestinthecontainmenthierarchy)positionedancestorelement.Apositionedancestoriseither:anelementwit......
  • Failed to load property source from location ‘classpath:/bootstrap.yml‘
    日常报错之无中生有,这个错误实属低级,原因是因为bootstrap.yml文件格式错误。仔细检查下格式是否有重的地方错误配置实例因为两个spring应该都归属到spring下边server:port:9001#指定开发环境spring:这里写错了。应该放到下边application:name:sys#指向......
  • Object.defineProperty(obj,key,val)不可以监听数组变化,需要做特殊处理,所以Vue3.0使用
    关于Vue双向数据绑定说法错误的是()AVue实现双向数据绑定是采用数据劫持和发布者-订阅者模式BObject.defineProperty(obj,key,val)可以监听数组变化,不需要做特殊处理CVue2.0数据劫持是利用ES5的Object.defineProperty(obj,key,val)方法来劫持每个属性的getter和setterD......
  • 对象定义 Object.create Object.defineProperty
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-......
  • CodeForces 852C Property
    洛谷传送门CF传送门NOIP模拟赛T1,小清新几何题。要让选出的点组成的多边形面积最大,就要让正多边形的面积减去选出的点组成的多边形面积最小。而这个面积差可以表示成\(2n\)个三角形的面积,即\(\sum\limits_{i=0}^{2n-1}S_{\triangleA_iA_{(i+1)\bmodn}B_{(i+......
  • spring复习:(57)PropertyOverrideConfigurer用法及工作原理
    一、属性配置文件dataSource.url=jdbc:mysql://xxx.xxx.xxx.xxx/testdataSource.username=rootdataSource.password=xxxxxxdataSource.driverClassName=com.mysql.jdbc.Driver#dataSource.type=com.alibaba.druid.pool.DruidDataSource二、spring配置文件<?xmlversion="1.0&quo......
  • Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理
    PropertySourcePlaceholderConfigurer的用途:通过配置文件(比如.properties文件)给bean设置属性,替代属性占位符示例:属性配置文件spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx/testspring.datasource.username=rootspring.datasource.password=xxxxxxspring.datasource.dri......
  • Uncaught TypeError: Cannot read property ‘addEventListener‘ of null 求助!!!!!!
    今天在项目中遇到个问题如下:vue项目中public的index.html文件script标签引入了一个外部的js文件,里面有一个方法每次调用的时候都会报错UncaughtTypeError:Cannotreadproperty‘addEventListener‘ofnull,网上查的所有办法都试过了:跟标签摆放先后位置,放到onload方法中都没......
  • INotifyPropertyChanged
      可以将TextBox控件(其他控件也基本一样)与某个变量进行绑定,做出改变变量则控件也跟着改变的效果。  首先需要声明一个类,该类用来与控件绑定:usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Runtime.CompilerServices;namespaceTestWPF{......