Skip to content

Commit cd4356d

Browse files
Update delattr.md
Added 2 examples of using delatrr() function
1 parent 240f463 commit cd4356d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/builtin/delattr.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ Python delattr() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19+
## Examples
20+
21+
```python
22+
# Example 1
23+
class Person:
24+
def __init__(self, name, age):
25+
self.name = name
26+
self.age = age
27+
28+
person = Person("John", 30)
29+
delattr(person, 'age')
30+
print(person.__dict__) # Output: {'name': 'John'}
31+
32+
# Example 2
33+
class Car:
34+
def __init__(self, make, model):
35+
self.make = make
36+
self.model = model
37+
38+
car = Car("Toyota", "Corolla")
39+
try:
40+
delattr(car, 'year')
41+
except AttributeError as e:
42+
print(f"Error: {e}") # Output: Error: 'Car' object has no attribute 'year'
43+
```
1944
<!-- remove this tag to start editing this page -->
2045
<empty-section />
2146
<!-- remove this tag to start editing this page -->

0 commit comments

Comments
 (0)