📜  Python property()(1)

📅  最后修改于: 2023-12-03 15:04:07.984000             🧑  作者: Mango

Python property()

The property() method in Python provides a way to encapsulate the getter and setter methods of an attribute. It allows the programmer to define a property, which can be called and worked with just like an attribute, but underneath the hood, it executes a getter or setter method.

Syntax
property(fget=None, fset=None, fdel=None, doc=None)
Parameters
  • fget - function to get the value of the attribute.
  • fset - function to set the value of the attribute.
  • fdel - function to delete the attribute.
  • doc - string that represents the documentation of the attribute.
Return Value

The property() method returns a property object.

How it works

The property() method takes one or more methods as arguments, which are used to define the property's behavior. These methods are used to get, set, and delete the value of the property.

Example 1: Using property() to define a class attribute that cannot be modified
class MyClass:
  def __init__(self):
    self._data = "Hello, world!"

  @property
  def data(self):
    return self._data

obj = MyClass()
print(obj.data)         # Output: Hello, world!
obj.data = "Goodbye!"   # Throws an AttributeError

In this example, we define a class MyClass with an attribute data. The attribute is read-only, so we don't provide a setter method. Instead, we use the @property decorator to define the getter method. This method simply returns the value of the _data attribute. When we call obj.data, it returns the string "Hello, world!".

However, if we try to change the value of obj.data by assigning to it, we get an AttributeError. This is because there is no setter method defined for the data property.

Example 2: Using property() to define a class attribute that can be modified
class MyClass:
  def __init__(self):
    self._data = "Hello, world!"

  @property
  def data(self):
    return self._data

  @data.setter
  def data(self, value):
    self._data = value

obj = MyClass()
print(obj.data)        # Output: Hello, world!
obj.data = "Goodbye!"
print(obj.data)        # Output: Goodbye!

In this example, we define a class similar to the previous one. However, in addition to the @property decorator, we also define a setter method for the data property. This method takes a value as an argument and sets the _data attribute to that value. We use the @data.setter decorator to associate this method with the data property.

Now, when we call obj.data, it returns the string "Hello, world!". But when we set obj.data = "Goodbye!", the _data attribute is updated to the new value. Calling print(obj.data) now outputs "Goodbye!".

Conclusion

The property() method in Python is a powerful tool for encapsulating the behavior of class attributes. By defining getter, setter, and deleter methods, we can define properties that behave like regular attributes, but provide additional functionality underneath the hood. This makes our code more robust and easier to maintain.