最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python中@property和property函數(shù)常見使用方法示例

 更新時間:2019年10月21日 09:35:09   作者:以夢為馬越騎越傻  
這篇文章主要介紹了python中@property和property函數(shù)常見使用方法,結(jié)合實例形式分析了Python @property和property函數(shù)功能、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下

本文實例講述了python中@property和property函數(shù)常見使用方法。分享給大家供大家參考,具體如下:

1、基本的@property使用,可以把函數(shù)當(dāng)做屬性用

class Person(object):
  @property
  def get_name(self):
    print('我叫xxx')
def main():
  person = Person()
  person.get_name
if __name__ == '__main__':
  main()

運行結(jié)果:

我叫xxx

2、@property的set,deleter,get

class Goods(object):
  @property
  def price(self):
    print('@property')
  @price.setter
  def price(self,value):
    print('@price.setter:'+str(value))
  @price.deleter
  def price(self):
    print('@price.deleter')
obj = Goods()
obj.price = 50
obj.price
del obj.price

運行結(jié)果:

@price.setter:50
@property
@price.deleter

3、@property demo

class Goods(object):
  def __init__(self):
    #原價
    self.original_price = 100
    #折扣
    self.discount = 0.8
  @property
  def price(self):
    #實際價格=原價*折扣
    new_price = self.original_price*self.discount
    return new_price
  @price.setter
  def price(self,value):
    self.original_price = value
  @price.deleter
  def price(self):
    del self.original_price
obj = Goods()
obj.price
obj.price = 200
del obj.price

4、property函數(shù)使用

class Foo(object):
  def get_name(self):
    print('get_name')
    return 'laowang'
  def set_name(self, value):
    '''必須兩個參數(shù)'''
    print('set_name')
    return 'set value' + value
  def del_name(self):
    print('del_name')
    return 'laowang'
  NAME = property(get_name, set_name, del_name, 'description.')
obj = Foo()
obj.NAME  #調(diào)用get方法
obj.NAME = 'alex'  #調(diào)用set方法
desc = Foo.NAME.__doc__   #調(diào)用第四個描述
print(desc)
del obj.NAME  #調(diào)用第三個刪除方法

運行結(jié)果:

get_name
set_name
description.
del_name

5、property函數(shù)操作私有屬性的get和set方法

class Person(object):
  def __init__(self, age):
    self.__age = age
  def set_age(self, value):
    self.__age = value
  def get_age(self):
    return self.__age
  AGE = property(get_age, set_age)
person = Person(15)
person.AGE = 20
print(str(person.AGE))

運行結(jié)果:

20

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • 使用Python代碼實現(xiàn)Linux中的ls遍歷目錄命令的實例代碼

    使用Python代碼實現(xiàn)Linux中的ls遍歷目錄命令的實例代碼

    這次我就要試著用 Python 來實現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡單,需要的朋友可以參考下
    2019-09-09
  • 學(xué)習(xí)python如何處理需要登錄的網(wǎng)站步驟

    學(xué)習(xí)python如何處理需要登錄的網(wǎng)站步驟

    這篇文章主要為大家介紹了python如何處理需要登錄的網(wǎng)站步驟學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • Python實現(xiàn)的石頭剪子布代碼分享

    Python實現(xiàn)的石頭剪子布代碼分享

    這篇文章主要介紹了Python實現(xiàn)的石頭剪子布代碼分享,本文和另一篇JavaScript實現(xiàn)的石頭剪刀布游戲源碼是姐妹篇,需要的朋友可以參考下
    2014-08-08
  • 使用PyCharm安裝pytest及requests的問題

    使用PyCharm安裝pytest及requests的問題

    這篇文章主要介紹了使用PyCharm安裝pytest及requests的相關(guān)資料,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • python根據(jù)時間獲取周數(shù)代碼實例

    python根據(jù)時間獲取周數(shù)代碼實例

    這篇文章主要介紹了python根據(jù)時間獲取周數(shù),通過周數(shù)獲取時間代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • python 生成xml文件,以及美化的實例代碼

    python 生成xml文件,以及美化的實例代碼

    這篇文章主要介紹了python 生成xml文件,以及美化的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Python字典推導(dǎo)式將cookie字符串轉(zhuǎn)化為字典解析

    Python字典推導(dǎo)式將cookie字符串轉(zhuǎn)化為字典解析

    這篇文章主要介紹了Python字典推導(dǎo)式將cookie字符串轉(zhuǎn)化為字典解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Python 照片人物背景替換的實現(xiàn)方法

    Python 照片人物背景替換的實現(xiàn)方法

    本文主要介紹了如何通過Python實現(xiàn)照片中人物背景圖的替換,甚至可以精細(xì)到頭發(fā)絲,感興趣的小伙伴可以看看
    2021-11-11
  • pandas DataFrame mask的具體使用

    pandas DataFrame mask的具體使用

    pandas.DataFrame.mask方法提供了一種靈活的方式來根據(jù)條件篩選和替換 DataFrame中的元素,本文主要介紹了pandas DataFrame mask的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2025-04-04
  • Python自動化操作Excel方法詳解(xlrd,xlwt)

    Python自動化操作Excel方法詳解(xlrd,xlwt)

    Excel是Windows環(huán)境下流行的、強大的電子表格應(yīng)用。本文將詳解用Python利用xlrd和xlwt實現(xiàn)自動化操作Excel的方法詳細(xì),需要的可以參考一下
    2022-06-06

最新評論

甘肃省| 鹤峰县| 洪雅县| 当阳市| 南城县| 广元市| 普兰县| 霍林郭勒市| 霍邱县| 新竹市| 睢宁县| 财经| 赣榆县| 平邑县| 疏附县| 青田县| 青铜峡市| 仁寿县| 高密市| 沽源县| 沁水县| 喜德县| 长顺县| 江都市| 富蕴县| 克拉玛依市| 玉田县| 应城市| 兴山县| 永城市| 镇沅| 光泽县| 呼和浩特市| 忻城县| 万安县| 疏附县| 五大连池市| 台中县| 嘉善县| 祁东县| 边坝县|