|
| 1 | +--- |
| 2 | +Title: 'property()' |
| 3 | +Description: 'Declares a range of functions to manipulate class attributes.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Methods' |
| 9 | + - 'Classes' |
| 10 | + - 'Functions' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`property()`** function is a built-in Python function used to define methods that get, set, and delete class attributes. It can be used as a [decorator](https://www.codecademy.com/resources/docs/python/decorators) or assigned to a class attribute. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +property(fget, fset, fdel, doc) |
| 22 | +``` |
| 23 | + |
| 24 | +The `property()` function requires four parameters: |
| 25 | + |
| 26 | +- `fget`: function to get the value of the attribute. |
| 27 | +- `fset`: function to set the value of the attribute. |
| 28 | +- `fdel`: function to delete the value of the attribute. |
| 29 | +- `doc`: string that creates `docstring` for the attribute. |
| 30 | + |
| 31 | +> **Note:** If no arguments are given then the function returns a base property attribute without any getter, setter or deleter. |
| 32 | +
|
| 33 | +## Example |
| 34 | + |
| 35 | +The following example will demonstrate the use of `property()` function using the `Pet` class: |
| 36 | + |
| 37 | +```py |
| 38 | +class Pet: |
| 39 | + def __init__(self, name): |
| 40 | + self._name = name |
| 41 | + |
| 42 | + def get_name(self): |
| 43 | + return self._name |
| 44 | + |
| 45 | + def set_name(self, name): |
| 46 | + self._name = name |
| 47 | + |
| 48 | + def del_name(self): |
| 49 | + del self._name |
| 50 | + |
| 51 | + name = property(get_name, set_name, del_name) |
| 52 | + |
| 53 | +# Create an instance of Pet class |
| 54 | +dog = Pet("Bruno") |
| 55 | + |
| 56 | +# Access the name attribute using the property |
| 57 | +print(dog.name) |
| 58 | + |
| 59 | +# Set a new value for the name attribute |
| 60 | +dog.name = "Mars" |
| 61 | + |
| 62 | +# Access the updated name attribute |
| 63 | +print(dog.name) |
| 64 | + |
| 65 | +# Delete the name attribute |
| 66 | +del dog.name |
| 67 | +``` |
| 68 | + |
| 69 | +The code results in the following output: |
| 70 | + |
| 71 | +```shell |
| 72 | +Bruno |
| 73 | +Mars |
| 74 | +``` |
| 75 | + |
| 76 | +## Codebyte Example |
| 77 | + |
| 78 | +In this example the `property()` function is used to access the `radius` attribute of the `Circle` class. |
| 79 | + |
| 80 | +```codebyte/python |
| 81 | +class Flower: |
| 82 | + def __init__(self, color): |
| 83 | + self._color = color |
| 84 | +
|
| 85 | + def get_color(self): |
| 86 | + return self._color |
| 87 | +
|
| 88 | + def set_color(self, color): |
| 89 | + self._color = color |
| 90 | +
|
| 91 | + def del_color(self): |
| 92 | + del self._color |
| 93 | +
|
| 94 | + color = property(get_color, set_color, del_color) |
| 95 | +
|
| 96 | +# Create an instance of Flower |
| 97 | +flower = Flower("Blue") |
| 98 | +
|
| 99 | +# Access the color attribute using the property |
| 100 | +print(flower.color) |
| 101 | +
|
| 102 | +# Set a new value for the color attribute |
| 103 | +flower.color = "Red" |
| 104 | +
|
| 105 | +# Access the updated color attribute |
| 106 | +print(flower.color) |
| 107 | +
|
| 108 | +# Delete the radius attribute |
| 109 | +del flower.color |
| 110 | +``` |
0 commit comments