mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
fix(curriculum): ensure setters run during object initialization (#65113)
This commit is contained in:
+4
-2
@@ -50,10 +50,12 @@ Notice how we used `_radius` instead of radius inside the class. The underscore
|
||||
|
||||
To make a setter to create the radius, for example, you have to define another method with the same name and use `@<property_name>.setter` above it:
|
||||
|
||||
Using `self.radius` inside `__init__` ensures the setter runs during object creation, so invalid radius values are caught immediately.
|
||||
|
||||
```py
|
||||
class Circle:
|
||||
def __init__(self, radius):
|
||||
self._radius = radius
|
||||
self.radius = radius # Calling the setter
|
||||
|
||||
@property
|
||||
def radius(self): # A getter to get the radius
|
||||
@@ -90,7 +92,7 @@ A deleter runs custom logic when you use the del statement on a property. To cre
|
||||
```py
|
||||
class Circle:
|
||||
def __init__(self, radius):
|
||||
self._radius = radius
|
||||
self.radius = radius
|
||||
|
||||
# Getter
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user