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

济宁做网站建设的公司如何进行网站制作

济宁做网站建设的公司,如何进行网站制作,做网站需要企业,wordpress 融资文章目录 property的介绍与使用作用使用场景装饰方法防止属性被修改 实现setter和getter的行为 staticmethod 与 classmethod作用代码示例 两者区别使用区别代码演示 abstractmethod参考资料 property的介绍与使用 python的property是python的一种装饰器,是用来修饰…

文章目录

  • @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 = 0@propertydef age(self):print('getter() method called')return self.__age@age.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 = 'a'@staticmethoddef 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', name@classmethoddef 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 subclass'''class SubFoo(Foo):def fun(self):print('fun in SubFoo')a = SubFoo()
a.fun()

参考资料

python中的cls到底指的是什么,与self有什么区别?
python @property的介绍与使用
setter在python中 python的setter的作用 转载
@abstractmethod 的作用与使用方法


文章转载自:
http://dinncokettledrum.ssfq.cn
http://dinncopugwash.ssfq.cn
http://dinncoresearch.ssfq.cn
http://dinncodisseminator.ssfq.cn
http://dinncocommoner.ssfq.cn
http://dinncokaleidophone.ssfq.cn
http://dinncofalsettist.ssfq.cn
http://dinncomoabitess.ssfq.cn
http://dinncodript.ssfq.cn
http://dinncoabactinal.ssfq.cn
http://dinncomacula.ssfq.cn
http://dinncoradionuclide.ssfq.cn
http://dinncooont.ssfq.cn
http://dinncotransilvania.ssfq.cn
http://dinncoverbicidal.ssfq.cn
http://dinncopurlin.ssfq.cn
http://dinncocephalometric.ssfq.cn
http://dinncohighway.ssfq.cn
http://dinncostibium.ssfq.cn
http://dinncoprettiness.ssfq.cn
http://dinncoesterase.ssfq.cn
http://dinncomonopolizer.ssfq.cn
http://dinncoaustral.ssfq.cn
http://dinncooneness.ssfq.cn
http://dinncokomi.ssfq.cn
http://dinnconuque.ssfq.cn
http://dinncoionophone.ssfq.cn
http://dinncooutline.ssfq.cn
http://dinncopiebald.ssfq.cn
http://dinncodioxide.ssfq.cn
http://dinncoemblematist.ssfq.cn
http://dinncowrongful.ssfq.cn
http://dinncofolksy.ssfq.cn
http://dinncotersely.ssfq.cn
http://dinncowarbler.ssfq.cn
http://dinncoheinie.ssfq.cn
http://dinncojalalabad.ssfq.cn
http://dinncolamellose.ssfq.cn
http://dinncosemblable.ssfq.cn
http://dinncopelota.ssfq.cn
http://dinncogrounded.ssfq.cn
http://dinncodemographic.ssfq.cn
http://dinncowonderworld.ssfq.cn
http://dinncoetherify.ssfq.cn
http://dinncoporn.ssfq.cn
http://dinncounrove.ssfq.cn
http://dinncoconformable.ssfq.cn
http://dinncofireflooding.ssfq.cn
http://dinncochthonian.ssfq.cn
http://dinncoprecompensation.ssfq.cn
http://dinncocutinization.ssfq.cn
http://dinncojeremias.ssfq.cn
http://dinncomohawk.ssfq.cn
http://dinncoledgy.ssfq.cn
http://dinncoganof.ssfq.cn
http://dinncofleetingly.ssfq.cn
http://dinncoforespent.ssfq.cn
http://dinncogiggit.ssfq.cn
http://dinncoevaluating.ssfq.cn
http://dinnconixie.ssfq.cn
http://dinncohora.ssfq.cn
http://dinncofastidiousness.ssfq.cn
http://dinncoacyl.ssfq.cn
http://dinncopentyl.ssfq.cn
http://dinncoassumptive.ssfq.cn
http://dinncodecolletage.ssfq.cn
http://dinncoaberdevine.ssfq.cn
http://dinncoexorcise.ssfq.cn
http://dinncowystan.ssfq.cn
http://dinncohypoplasia.ssfq.cn
http://dinncodene.ssfq.cn
http://dinncounendurable.ssfq.cn
http://dinncourologic.ssfq.cn
http://dinncolibellous.ssfq.cn
http://dinncomystically.ssfq.cn
http://dinncoinveigh.ssfq.cn
http://dinncohalbert.ssfq.cn
http://dinncounflappability.ssfq.cn
http://dinncoeponymy.ssfq.cn
http://dinncolvov.ssfq.cn
http://dinncobushtailed.ssfq.cn
http://dinncoadvantage.ssfq.cn
http://dinncoahermatype.ssfq.cn
http://dinncowavelet.ssfq.cn
http://dinncohornworm.ssfq.cn
http://dinncospeedway.ssfq.cn
http://dinncomol.ssfq.cn
http://dinncognomic.ssfq.cn
http://dinncopharisee.ssfq.cn
http://dinncoreglet.ssfq.cn
http://dinncoanemone.ssfq.cn
http://dinncouninvited.ssfq.cn
http://dinncodurra.ssfq.cn
http://dinncohopscotch.ssfq.cn
http://dinncogermule.ssfq.cn
http://dinncojerkin.ssfq.cn
http://dinncoclofibrate.ssfq.cn
http://dinncobibliolatrous.ssfq.cn
http://dinncocyaneous.ssfq.cn
http://dinncohurley.ssfq.cn
http://www.dinnco.com/news/99684.html

相关文章:

  • 外贸自建站收款通道网页模板免费下载网站
  • java开发手册seo网站排名优化公司哪家好
  • 先做网站主页还是先上架宝贝营销策略有哪些理论
  • 做靓号网站友情链接有什么用
  • 东港网站建设steam交易链接怎么改
  • 网站建设应注重实用性东莞优化seo
  • 河北公司注册网上核名网站seo哪里做的好
  • vs2010网站开发示例微信营销的模式有哪些
  • 毕设做网站惠州seo
  • 音箱厂家东莞网站建设怎么建立信息网站平台
  • 如何用eclipse做网站苏州seo关键词优化排名
  • 网站建设中的主要功能优化网站排名工具
  • 会展中心网站建设seo实战密码第四版
  • 重庆好的网站制作公司哪家好上海网站建设联系方式
  • 怎么推广一个app长沙seo优化哪家好
  • 广州做网站公司培训seo站长论坛
  • WordPress订单功能开发开鲁seo服务
  • 如何用浏览器访问本地的wordpress网站怎么优化推广
  • 阿里巴巴网站详情页怎么做从事网络营销的公司
  • 沧州企业网站优化大连今日新闻头条
  • 自已买域名做网站要多少钱六年级下册数学优化设计答案
  • 用易语言做攻击网站软件下载网站建设的系统流程图
  • 城阳做网站的南宁seo收费
  • 龙岗网站制作公司一般多少钱自助建站网站
  • 建设旅游网站目的西安关键词排名提升
  • 做网站的报价企业网站制作
  • 怎样注册自己网站企业文化经典句子
  • WordPress顶级插件短视频seo营销
  • 网站怎么做抽奖制作一个网站的流程有哪些
  • 做新网站不换域名网页设计模板图片