当前位置: 首页 > news >正文

最新某地方装修门户源码 php装饰公司程序 dede行业网站模板宝塔面板wordpress数据库

最新某地方装修门户源码 php装饰公司程序 dede行业网站模板,宝塔面板wordpress数据库,创业商机网官网,上海市建筑网文章目录 property的介绍与使用作用使用场景装饰方法防止属性被修改 实现setter和getter的行为 staticmethod 与 classmethod作用代码示例 两者区别使用区别代码演示 abstractmethod参考资料 property的介绍与使用 python的property是python的一种装饰器#xff0c;是用来修饰… 文章目录 property的介绍与使用作用使用场景装饰方法防止属性被修改 实现setter和getter的行为 staticmethod 与 classmethod作用代码示例 两者区别使用区别代码演示 abstractmethod参考资料 property的介绍与使用 python的property是python的一种装饰器是用来修饰方法的。 作用 我们可以使用property装饰器来创建只读属性property装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用这样可以防止属性被修改。 使用场景 装饰方法 修饰方法是方法可以像属性一样访问。如果使用property进行修饰后又在调用的时候方法后面添加了() 那么就会显示错误信息TypeError: ‘int’ object is not callable也就是说添加property 后这个方法就变成了一个属性如果后面加入了()那么就是当作函数来调用而它却不是callable可调用的。没有使用property修饰它是一种方法如果把括号去掉不会报错输出的就会是方法存放的地址。代码示例 class DataSet(object):propertydef method_with_property(self): ##含有propertyreturn 15def method_without_property(self): ##不含propertyreturn 15l DataSet() print(l.method_with_property) # 加了property后可以用调用属性的形式来调用方法,后面不需要加。 print(l.method_without_property()) #没有加property , 必须使用正常的调用方法的形式即在后面加()防止属性被修改 ​ 由于python进行属性的定义时没办法设置私有属性因此要通过property的方法来进行设置。这样可以隐藏属性名让用户进行使用的时候无法随意修改。 class DataSet(object):def __init__(self):self._images 1self._labels 2 #定义属性的名称propertydef images(self): #方法加入property后这个方法相当于一个属性这个属性可以让用户进行使用而且用户有没办法随意修改。return self._images propertydef labels(self):return self._labels l DataSet() #用户进行属性调用的时候直接调用images即可而不用知道属性名_images因此用户无法更改属性从而保护了类的属性。 print(l.images) # 加了property后可以用调用属性的形式来调用方法,后面不需要加。实现setter和getter的行为 property是python的一个内置装饰器使用装饰器的目的是改变类的方法或者属性这样调用者就无需在代码中做任何改动。 class Adult(object): def __init__(self):self.__age 0propertydef age(self):print(getter() method called)return self.__ageage.setterdef age(self, value):if value 18:raise ValueError(Sorry, you are a child, games not allowed)print(setter() method called)self.__age valuexiaoli Adult() xiaoli.age 19 print(xiaoli.age)staticmethod 与 classmethod 作用 一般来说要使用某个类的方法需要先实例化一个对象再调用方法。而使用staticmethod或classmethod就可以不需要实例化直接类名.方法名()来调用。这有利于组织代码把某些应该属于某个类的函数给放到那个类里去同时有利于命名空间的整洁。 代码示例 分别定义静态函数类普通函数类函数 class A(object):a a# foo1为静态函数用staticmethod装饰器装饰staticmethoddef foo1(name):print hello, name# foo2为正常的函数是类的实例的函数def foo2(self, name):print hello, name# foo3为类函数classmethoddef foo3(cls, name):print hello, name调用 a A() a.foo1(mamq) # 输出: hello mamq A.foo1(mamq)# 输出: hello mamq a.foo2(mamq) # 输出: hello mamq A.foo2(mamq) # 报错: unbound method foo2() must be called with A instance as first argument (got str instance instead) a.foo3(mamq) # 输出: hello mamq A.foo3(mamq) # 输出: hello mamq两者区别 使用区别 staticmethod不需要表示自身对象的self和自身类的cls参数就跟使用函数一样。classmethod也不需要self参数但第一个参数需要是表示自身类的cls参数。如果在staticmethod中要调用到这个类的一些属性方法只能直接类名.属性名或类名.方法名。而classmethod因为持有cls参数可以来调用类的属性类的方法实例化对象等避免硬编码。 代码演示 在classmethod中可以调用类中定义的其他方法、类的属性但staticmethod只能通过A.a调用类的属性但无法通过在该函数内部调用A.foo2()。 class A(object):a astaticmethoddef foo1(name):print hello, nameprint A.a # 正常print A.foo2(mamq) # 报错: unbound method foo2() must be called with A instance as first argument (got str instance instead)def foo2(self, name):print hello, nameclassmethoddef foo3(cls, name):print hello, nameprint A.aprint cls().foo2(name)abstractmethod 如果在父类中某个函数使用了abstractmethod那么子类中必须重新定义一遍这个函数其子类才能实例化否则就会报错。使用abstractmethod的作用是提醒编程者避免子类在编程过程中漏掉某个必要的函数。代码示例 from abc import ABC, abstractmethodclass Foo(ABC):abstractmethoddef fun(self):please Implemente in subclassclass SubFoo(Foo):def fun(self):print(fun in SubFoo)a SubFoo() a.fun()参考资料 python中的cls到底指的是什么与self有什么区别? python property的介绍与使用 setter在python中 python的setter的作用 转载 abstractmethod 的作用与使用方法
http://www.w-s-a.com/news/520188/

相关文章:

  • 中新生态城建设局门户网站wordpress云盘视频播放
  • 大型网站开发基本流程wordpress记录用户搜索
  • 云服务器安装win系统做网站wordpress边栏扩大尺寸
  • 网站开发面试自我介绍软件下载网站如何建设
  • 可以做翻译任务的网站陕西省建设厅八大员证
  • 昆明 网站推广重庆网页优化seo公司
  • 网站排名下降怎么上去设计一套app页面多少钱
  • 专门用来查找网址的网站查公司名字是否被注册
  • 自己创建网站教程河南省建设厅官方网站李学军
  • 一个网站需要多少容量怎样免费设计网站建设
  • 建设工程交易中心网站12306的网站是哪个公司做的
  • 建设网站经营范围自己给公司做网站
  • 河北省住房建设厅政务网站网络营销推广的岗位职责有哪些
  • 上海网站建设优化价格孝义做网站的公司
  • 哪个公司网站做的最好义乌 网站 制作
  • 百度站长工具综合查询wordpress 上传pdf
  • 旅游短租公寓网站建设深圳龙岗招聘网
  • 做海淘是在哪个网站网络查控系统设计方案
  • o2o网站建设代理商微信公众号开发文档
  • 网站设计课程总结关于网站备案的公告
  • 网站建设与运营意义到哪查找网站域名
  • 网站及单位网站建设情况眉县住房和城市建设局网站
  • 网站是否能够被恶意镜像wordpress占用
  • 经典设计网站网站等保测评怎么做
  • 重庆做网站公司贴吧廊坊公司快速建站
  • 海外贸易在什么网站做怎么排名到百度第一页
  • 线上注册公司是在哪个网站做高仿网站
  • 网站构架图网上推广平台哪个好
  • 公司网站首页图片素材vi设计的目的和意义
  • 网站的需求分析都有哪些内容济南营销型网站建设团队