python的Getter和Setter
#python #code #oops #pythonbeginnerfriendly

在面向对象的程序中Getter和Setter的主要目的是确保数据封装或隐藏。但是在Python Getter和Setter中,由于Python中的私人变量并没有真正隐藏,并且可以从外部访问。

作为结果,Getter和Setter通常用于设置,但验证和逻辑是类字段并避免直接访问。

使用Python

可以通过多种方式实现此GETER和SETETER属性
  • 使用具有预定义命名法的正常函数

  • 使用属性()方法python内置函数

  • 使用@property装饰器

将正常函数与预定义的命名法

根据命名法,使用 double undescor e的任何功能或变量是类的私人功能。

示例:

__name = 'vinayak'
## or
def __my_name():
    return "vinayak"

以及前面的任何get和set Append的方法都被视为该变量的Getter和setter方法。

class Student(object):
    def __init__ (self, name: str) -> None:
        self.name: str = name
        self.__marks: int = 0

    def set_marks(self, marks: int):
        """
        setter method
        Set Method in python
        """
        self.__marks = marks

    def get_marks(self):
        """
        getter method
        Get Method in python
        """
        return self.name + " got " + str(self.__marks)

    def __str__ (self) -> str:
        return "student name is " + self.name

s = Student("vinayak")
print(s)
s.set_marks(10)
marks_s_got = s.get_marks()
print(marks_s_got)

输出:

student name is vinayak

vinayak got 10

使用属性()方法Python内置功能

property()方法在Python中获取四个参数 fget fset fdel doc 其中fget用于检索属性值,使用FSET来设置属性的值,而FDEL用于删除属性值,而DOC只是属性的DocString。

class Student(object):
    def __init__ (self, name: str) -> None:
        self.name: str = name
        self.__marks: int = 0

    def set_marks(self, marks: int):
        """
        setter method
        Set Method in python
        """
        self.__marks = marks

    def get_marks(self):
        """
        getter method
        Get Method in python
        """
        return self.name + " got " + str(self.__marks)

    def del_marks(self):
        """
        Del Method
        """
        self.__marks = 0
        return 1

    def __str__ (self) -> str:
        return "student name is " + self.name

    docString = "getter and setter for marks variable"
    marks = property(get_marks, set_marks, del_marks, docString)

s = Student("vinayak")
print(s)
s.marks = 10
marks_s_got = s.marks
print(marks_s_got)
del s.marks
print(s.marks)

输出:

student name is vinayak

vinayak got 10

vinayak got 0

使用@property装饰师

正如我们已经看到属性()方法的那样,属性装饰者也可以实现相同的行为。 @property装饰器是Python的内置装饰器之一。

class Student(object):
    def __init__ (self, name: str) -> None:
        self.name: str = name
        self.__marks: int = 0

    @property
    def marks(self):
        """
        getter method
        Get Method in python
        """
        return self.name + " got " + str(self.__marks)

    @marks.setter
    def marks(self, marks: int):
        """
        setter method
        Set Method in python
        """
        self.__marks = marks

    @marks.deleter
    def marks(self):
        """
        Del Method
        """
        self.__marks = 0
        return 1

    def __str__ (self) -> str:
        return "student name is " + self.name

s = Student("vinayak")
print(s)
s.marks = 10
marks_s_got = s.marks
print(marks_s_got)
del s.marks
print(s.marks)

输出:

student name is vinayak

vinayak got 10

vinayak got 0