Python Deleter
Python users generally remove items by using the del function. The del function also has the ability to delete a list, slice a list, remove a dictionary, remove unwanted key-value pairs out of a dictionary, remove attributes, etc. because each item in Python describes each object in some way.
What is Python Deleter?
A class’s property can be deleted using the deleter function in Python. The property cannot be retrieved by the same instance after it has already been deleted. One thing to keep in mind is that an object’s property, not its class, is deleted using the deleter method in Python. To understand Python deleter, we must first learn Python classes. In this article, we’ll look at various Python deleter methods for deleting properties as well as how to make a class in Python.
Syntax: del object_name
Python deleter property decorator
The decorator is one of Python’s most vital functions. It consists of a group of functions that take another function argument and modify it before returning it.
A type of decorator is the property, which is typically represented as @propertry. Utilize the @property decorator if you wish to use a function like a property or an attribute. Under the property() function, it is a built-in function. In order to define the properties of the Python class, the property() function can be used. The get, set, and delete methods are passed as arguments to the property() method, which returns an object of the property class.
There are mainly three methods associated with a property in Python:
- Python getter -The value of the attribute can be accessed using it.
- Python setter – The value of the attribute is set using it.
- Python deleter – The object attribute is removed using it.
What is Getter in Python?
In object-oriented programming (OOPS), the methods known as getters have been used to get a class’s internal properties. The Python getattr() function is identical to the setattr() function. This then modifies the properties of an object’s variables.
What is Setter in Python?
The value of the attribute is defined using the setter method. In object-oriented programming, the capability to modify each and every variable from a class’ internal variables is still an extremely useful feature.
Using the normal function to achieve getters and setters behavior:
When creating the getters & setters properties, we can define standard get() and set() methods without requiring any extra functionality.
Example
Therefore, to know why we can implement the Python getters and setters methods by using functional capacity, now let us consider the example.
class students: def __init__(self, age = 0): self._age = age # using the getter method def get_age(self): return self._age # using the setter method def set_age(self, a): self._age = a David = students() #using the setter function David.set_age(22) # using the getter function print(David.get_age()) print(David._age)
Output
22 22
What are getters and setters in Python?
Python getters are the methods used in Object-Oriented Programming (OOPS) that help to access the private attributes from a class while Python setters are the methods used in the OOPS feature which help to set the value to private attributes of a class.
Let us create a class with some methods to understand how the getter and setter work in Python.
# creating the class class Person: # initializing def __init__(self, name): # private varibale or property in Python self.__name = name #getter method to get the property def get(self): return self.__name #setter method to change the value def set_a(self, name): self.__name = name
Now, we will create an object and use the getter method to get the name of the Person.
# creating an object person = Person("Andy") # getting the method person.get()
Output:
'Andy'
As you can see, the getter is used to get the property of the object. The setter is used to change the value of the property. For example, we will now change the name of a person using the setter method.
# setting a new value person.set_a('Mandy') # getting the value of model print(person.get())
Output:
Mandy
Using property() function to achieve getters and setters behavior
The built-in Python method property() generates and returns a property class. A property object has three methods: getter (), setter (), and delete (). Four arguments are provided here for the property() function in Python: property(fget, fset, fdel, doc); fget is a function for obtaining the parameter value. A function that changes an attribute’s value is called fset. A method called fdel is used to remove a variable. Doc generates a docstring for a single attribute. The three methods getter(), setter(), and delete() on a property object allow you to provide fget, fset, and fdel separately.
Syntax: property(fget, fset, fdel, doc)
Where the parameters mean:
fget(): used to get the value of the attribute
fset(): used to set the value of the attribute
fdel(): used to delete the attribute value
doc(): a string that contains the documentation (docstring) for the attribute
class Student: def __init__(self, fname, lname) def: self.fname = fname self.lname = lname self.fullname = fname+' '+lname def fullname_getter(self): return self.fname +' '+ self.lname def fullname_setter(self,name): firstname, lastname = name. split() self.fname = firstname self.lname = lastname def fullname_deleter(self): self.fname = None self.lname = None print('Deleted the fullname.') def email(self): return '{}.{}@email.com'.format(self.fname, self.lname) fullname = property() fullname = fullname.getter(fullname_getter) fullname = fullname.setter(fullname_setter) fullname = fullname.deleter(fullname_deleter) # this can be done in a single line too # fullname = property(fullname_getter, fullname_setter, fullname_deleter) s1 = Student('Tony', 'Stark')('Candy', 'Sandy') print('Fullname of s1 is ', s1.fullname) print('And email address = ', s1.email()) # now updating the s1 object's first name s1.first = 'Steve''Mark' print('Fullname of s1 is ', s1.fullname) print('And email address = ', s1.email()) #setting new value of fullname s1.fullname = 'Austin Waugh' print('New Fullname of s1 is ', s1.fullname) #deleting the fullname del s1.fullname
Output
Fullname of s1 is Candy Sandy And email address = Candy.Sandy@studytonight.com Fullname of s1 is Mark Sandy And email address = Mark.Sandy@studytonight.com New fullname of s1 is Austin waugh Deleted the fullname.
Now We Understand Python Deleter
Python’s deleter function is used to remove an object’s property. Being careful that only the Python deleter, in this case, deletes an object’s property instead of the class property. The deleter function in Python can be used to remove an object’s property. Using various examples, we will discuss how to do this in the above chapter. The different steps mentioned below will be mainly used to build Python deleters.
There are two different functional deleters.
1. Python Deleter with Methods
2. Python Deleter with Decorators
3. Python Deleter Without Decorators
Python Deleter with Methods
Now, let’s define a standard class with setter, getter, and Python deleter methods.
# creating the class class Student: # initializing def __init__(self, name): # private varibale or property in Python self.__name = name #getter method to get the property def get(self): return self.__name #setter method to change the value def set_a(self, name): self.__name = name # creating python deleter def deleter(self): del self.name
You see that we now built the class “Student” with a setter, getter, and Python deleter that removes the student’s name.
Now that we have specified the student’s name, let’s build an object.
# creating an object student1 = Student("Sam") # accessing the name of the student print(student1.get())
Output
Sam
# calling python deleter student1.deleter() # printing the name print(student1.get())
Output
AttributeError (Trace back most recent call) temp/apk_kernel/38789_68436578.py in <module> #1 calling python deleter #2 student.deleter() #3 #4 printing the name() #5 print(student1.get()) temp/apk_kernel/38789_68436755.py in deleter <safe> 18 # creating python deleter 19 def deleter(self): 20 del self.name AttributeError : name
Python deleter with decorators
A Python pattern called a decorator helps users add additional features to something like an object class without affecting the object’s construction. Decorators are commonly called before or during a stored procedure declaration, like @property, that you’re trying to decorate.
Before using Python’s deleter to remove an object’s property, let’s build a class with decorators.
Now, let us create an object and print out the name.
# creating a class class Person: def __init__(self, name): self._name = name # using decorator for property @property def name(self): return self._name # using decorators for setter @name.setter def name(self, value): self._name = value #using decorator for python deleter @name.deleter def name(self): del self._name
Now, let us create an object and print out the name.
# creating new object person = Person('Peter') # printing the name print(person.name)
output
Peter
Let’s say we don’t want the individual to own the name property. The name property is deleted using the Python deleter, as shown below:
# deleting the name del person.name # calling name print(person.name)
AttributeError (Trace back most recent call) temp/apk_kernel/38789_68436578.py in <module> 3 # 4 # calling name 5 # print(person.name) temp/apk_kernel/38789_68436755.py in name <self> 8 @property 9 def name(self) 10 return self._name 11 12 # using setter for decorator AttributeError : 'person' object no attributes "_name"
You see that when we want to open a property by using Python’s deleter to remove it, we receive an error.
Python deleter without decorators
By using decorators, let’s design a class that has getter, setter, and delete methods.
# creating the class class Person: # initializing def __init__(self, name): # private varibale or property in Python self.__name = name #getter method to get the property def get(self): return self.__name #setter method to change the value def set_a(self, name): self.__name = name # creating python deleter def deleter(self): del self.name
Now let’s build an object and print its name.
# creating an object person = Person("Brad") # accessing the name of the student print(person.get())
Output:
Brad
You’ll notice that we receive the name. Let’s now use Python deleter to remove the name property.
# calling python deleter person.deleter() # printing the name print(person.get())
output
AttributeError Trace back (most recent call) temp/apk_kernel/39646_6843645338.py in <module> 1 #calling python deleter 2 person.deleter() 3 4 # printing the name 5 # print(person.get()) temp/apk_kernel/39646_6843653463.py in deleter <self> 18 # creating python deleter 19 def deleter(self): 20 del self._name AttributeError : name
We now have an error because we used Python’s deleter to delete the property, as you can see.
Summary
The deleter function in Python is used to remove a class’s properties. The property cannot be accessed using the same instance after being deleted. In this short article, we covered how to delete an object’s property using Python’s deleter. Both with and without decorators and Methods, we utilized the Python deleter.