欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用property完成數(shù)據(jù)隱藏封裝與校驗(yàn)

 更新時(shí)間:2024年11月22日 09:16:54   作者:仙草哥哥  
這篇文章主要為大家詳細(xì)介紹了python使用property完成數(shù)據(jù)隱藏封裝與校驗(yàn),實(shí)現(xiàn)安全修改,文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助

禁止隨意設(shè)置屬性

需求

考慮這樣一種情況,在某一個(gè)類中,有一個(gè)屬性我們不希望別人隨便設(shè)置,因?yàn)檫@可能會(huì)造成很大的麻煩:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    
    def calculate_area(self):
        return self.width * self.height
 
 
r = Rectangle(3, 4)
print(r.calculate_area())

在這種情況下,長度和寬度都應(yīng)該是數(shù)字類型的,而如果有人在這個(gè)過程中修改了這個(gè)屬性,那么就會(huì)導(dǎo)致錯(cuò)誤。

r = Rectangle(3, 4)
r.width = "3"
print(r.calculate_area())  # 結(jié)果為3333,明顯不是我們希望的結(jié)果

私有屬性

為了避免他人隨意訪問,我們可以將其設(shè)置為私有屬性,通??梢蕴砑?個(gè)或者2個(gè)下劃線。

class Rectangle:
    def __init__(self, width, height):
        # 一個(gè)下劃線通常表明了,這個(gè)屬性不應(yīng)該隨意使用,但是不起實(shí)際作用
        self._width = width
        
        # 兩個(gè)下劃線表明了,這個(gè)屬性會(huì)觸發(fā)名稱重整,但是也并非真正的私有
        self.__height = height
        
        
r = Rectangle(3, 4)
print(r._width)
 
# 雙下劃線最終會(huì)重整為_類名__屬性名的形式
print(r._Rectangle__height)

需要注意的是,由于python中沒有真正的私有屬性,所以,這兩種設(shè)置方法都不能完全起到作用,但是一般而言,兩個(gè)下劃線是更好的,因?yàn)榭梢杂|發(fā)名稱重整。

提供訪問方法

在這種情況下,為了避免他人隨意設(shè)置屬性,因此需要提供專門的訪問方法。

class Rectangle:
    def __init__(self, width, height):
        self.set_width(width)
        self.set_height(height)
 
    def set_width(self, width):
        if not isinstance(width, (int, float)):
            raise ValueError("width must be a number.")
        if width <= 0:
            raise ValueError("width must be greater than 0.")
        self.__width = width
 
    def set_height(self, height):
        if not isinstance(height, (int, float)):
            raise ValueError("height must be a number.")
        if height <= 0:
            raise ValueError("height must be greater than 0.")
        self.__height = height
 
    def get_width(self):
        return self.__width
 
    def get_height(self):
        return self.__height
 
r = Rectangle(3, 4)
 
# 此時(shí)需要通過專門的方法進(jìn)行訪問
print(r.get_width())
 
# 非法的設(shè)置不再被允許
r.set_width("3")

但是這樣的使用方法并不方便,因此,在python中,一個(gè)更好的使用方法是通過property。

property的詳細(xì)介紹

基本語法

基本語法實(shí)現(xiàn)是通過property的內(nèi)置函數(shù)完成的

class MyClass:
    def __init__(self, value):
        self.value = value
 
    # get方法
    def get_value(self):
        return self.__value
 
    # set方法
    def set_value(self, value):
        if value < 0:
            raise ValueError("value must be non-negative.")
        self.__value = value
 
    # del方法
    def del_value(self):
        print("value is being deleted.")
        del self.__value
 
    # property創(chuàng)建
    value = property(get_value, set_value, del_value)
 
mc = MyClass(20241120)
print(mc.value)

使用裝飾器

class MyClass:
    def __init__(self, value):
        self.value = value
 
    @property
    def value(self):
        return self.__value
 
    @value.setter
    def value(self, value):
        if value < 0:
            raise ValueError("value must be non-negative.")
        self.__value = value
 
    @value.deleter
    def value(self):
        print("value is being deleted.")
        del self.__value

與基本語法的版本效果是相同的

常見應(yīng)用

數(shù)據(jù)隱藏與封裝

property裝飾器允許像訪問和操作普通一樣屬性進(jìn)行操作

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
 
    @property
    def width(self):
        return self.__width
 
    @width.setter
    def width(self, value):
        if not isinstance(value, (int, float)):
            raise ValueError("width must be a number.")
        if value <= 0:
            raise ValueError("width must be greater than 0.")
        self.__width = value
 
    @property
    def height(self):
        return self.__height
 
    @height.setter
    def height(self, value):
        if not isinstance(value, (int, float)):
            raise ValueError("height must be a number.")
        if value <= 0:
            raise ValueError("height must be greater than 0.")
        self.__height = value
 
        
r = Rectangle(3, 4)
print(r.width)
 
r.width = "3"

設(shè)置只讀屬性

通過property,可以設(shè)置一個(gè)屬性只允許讀取,不允許修改

class Person:
    def __init__(self, name):
        self.__name = name
 
    @property
    def name(self):
        return self.__name
    
# 初始化的時(shí)候可以設(shè)置名字
p = Person("sagegrass")
 
# 名字可以正常被訪問
print(p.name)
 
# 名字不可以被修改,AttributeError: can't set attribute 'name'
p.name = "xxx"

一般來說,這樣設(shè)置已經(jīng)足夠保證屬性只讀,但是極特殊的情況下,仍然可以通過p._Person__name進(jìn)行修改,

不過通常無需額外關(guān)注這一問題。

動(dòng)態(tài)計(jì)算

關(guān)于一開始的例子,我們使用calculate_area進(jìn)行面積的計(jì)算,但是,通過property就可以將area設(shè)置為一個(gè)屬性,進(jìn)行動(dòng)態(tài)的計(jì)算

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    
    def calculate_area(self):
        return self.width * self.height

使用property屬性,修改為:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
 
    @property
    def area(self):
        return self.width * self.height
    
r = Rectangle(3, 4)
print(r.area)
r.width = 5
print(r.area)

以上就是python使用property完成數(shù)據(jù)隱藏封裝與校驗(yàn)的詳細(xì)內(nèi)容,更多關(guān)于python property數(shù)據(jù)隱藏封裝與校驗(yàn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論