Home  >  Article  >  Backend Development  >  Introduction to Python functions: Introduction and examples of delattr function

Introduction to Python functions: Introduction and examples of delattr function

PHPz
PHPzOriginal
2023-11-03 10:30:111158browse

Introduction to Python functions: Introduction and examples of delattr function

Introduction to Python functions: Introduction and examples of delattr function

As an advanced programming language, Python has a rich built-in function library and provides many convenient and fast functions. functions to perform various operations. One of them is the delattr function. This article will introduce the role and usage of delattr function in detail, and attach specific code examples.

The delattr function is a built-in function in Python, which is used to delete the attributes of an object. In Python, an object's properties are variables stored in the object. Normally, we can use the dot operator to access the attributes of an object, such as obj.attr. The delattr function is used to delete these attributes.

The usage of delattr function is very simple. It accepts two parameters. The first parameter is the object of the attribute to be deleted, and the second parameter is the name of the attribute to be deleted. The specific syntax is as follows:

delattr(object, name)

where object is the object whose attribute is to be deleted, and name is the name of the attribute to be deleted.

Next, we will demonstrate the specific usage of delattr function through examples.

Example 1:

class Person:

def __init__(self, name, age):
    self.name = name
    self.age = age

p = Person("Tom", 20)

print("Attribute before deletion:" )
print("Name:", p.name)
print("Age:", p.age)

Delete attribute

delattr(p, "name" )

print("Deleted attribute:")

Accessing a deleted attribute will cause an AttributeError exception

print("Name:", p.name)
print("Age:", p.age)

Run the above code, the results are as follows:

Attributes before deletion:
Name: Tom
Age: 20
Attribute after deletion:
Traceback (most recent call last):
File "main.py", line 17, in

print("姓名:", p.name)

AttributeError: 'Person' object has no attribute 'name'

In the example, we define a Person class that has two attributes: name and age. Then we create a Person object p and print out the attributes before deletion. Next, we use the delattr function to delete the name attribute of the p object and print out the deleted attribute. Since the name attribute has been successfully deleted, an AttributeError exception is raised when accessing the name attribute.

Example 2:

class Car:

def __init__(self, brand, model, price):
    self.brand = brand
    self.model = model
    self.price = price

c = Car("Tesla", "Model S", 100000)

print("Delete Attributes before: ")
print("Brand:", c.brand)
print("Model:", c.model)
print("Price:", c.price)

Delete attributes

delattr(c, "price")

print("Deleted attributes:")
print("Brand:", c.brand)
print("Model:", c.model)

Raises AttributeError exception

print("Price:", c.price)

Run the above code, The results are as follows:

Attributes before deletion:
Brand: Tesla
Model: Model S
Price: 100000
Attributes after deletion:
Brand: Tesla
Model: Model S
Traceback (most recent call last):
File "main.py", line 20, in

print("价格:", c.price)

AttributeError: 'Car' object has no attribute ' price'

In Example 2, we define a Car class, which has three attributes: brand, model and price. Then we created a Car object c and printed out the properties before deletion. Next, we use the delattr function to delete the price attribute of the c object and print out the deleted attribute. Likewise, since the price attribute has been successfully deleted, when accessing the price attribute, an AttributeError exception is raised.

Through the above example, we can see that the delattr function can conveniently delete the attributes of an object. This is very useful in certain scenarios. For example, when we no longer need an attribute, we can use the delattr function to delete it, thereby reducing memory consumption.

Summary:

The delattr function is a built-in function in Python, used to delete the attributes of an object. Its syntax is very simple and accepts two parameters. The first parameter is the object of the attribute to be deleted, and the second parameter is the name of the attribute to be deleted. Use the delattr function to easily delete the attributes of an object and reduce memory consumption.

The above is the detailed content of Introduction to Python functions: Introduction and examples of delattr function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn