程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python property function

編輯:Python

One 、 No, property function

         Generally in object-oriented programming , Private properties are not exposed , But through an interface function to set , Usually, a private attribute corresponds to two function methods , You can also add a deletion method

class Test():
def __init__(self):
self.name= 'handong'
# Private property
self.__id = '001'
def get_id(self):
return self.__id
def set_id(self,value):
# First, carry out parameter detection , It can be used to check permissions
if isinstance(value,int):
self.__id = value
else:
print(' Incorrect input , Please re-enter !')
if __name__ == '__main__':
# Instantiation Test class , Create instantiated objects
t=Test()
# Access class properties
print(t.name)
# Access the private properties of the class
print(t.get_id())
# Change the private attribute value of the class
t.set_id(109)
print(t.get_id())

Two 、 Yes property function         

         When there are multiple private attributes , When setting and accessing private properties through the above methods , There are too many ways , External inconvenient call ,python in property Function to solve this problem , It is convenient for external calls and setting privatization properties .

Add a line of code :id = property(get_id,set_id)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved