TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python delattr() function with Examples

Python delattr() function with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python delattr() Function

Python delattr() function is used to delete an attribute from a class. It takes two parameters first is an object of the class and second is an attribute which we want to delete. After deleting the attribute, it no longer available in the class and throws an error if try to call it using the class object.

Signature

delattr (object, name)

Parameters

object: Object of the class which contains the attribute.

name: The name of the attribute to delete. It must be a string.

Return

It returns a complex number.

Let's see some examples of delattr() function to understand it's functionality.

Python delattr() Function Example 1

It is a simple example which contains a Student class, and by using delattr() function, we will delete it's email attribute.

# Python delattr() function example
class Student:
    id = 101
    name = "Rohan"
    email = "rohan@abc.com"
    def getinfo(self):
        print(self.id, self.name, self.email)
s = Student()
s.getinfo()
delattr(Student,'email') # Removing attribute
s.getinfo() # error: no attribute 'email' is available

Output:

AttributeError: 'Student' object has no attribute 'email'
101 Rohan [email protected]

Python delattr() Function Example 2

It throws an error if we delete an attribute which does not exist.

# Python delattr() function example
class Student:
    id = 101
    name = "Rohan"
    email = "rohan@abc.com"
# Declaring function
    def getinfo(self):
        print(self.id, self.name, self.email)
s = Student()
s.getinfo()
delattr(Student,'course') # Removing attribute which is not available
s.getinfo() # error: throws an error

Output:

AttributeError: course

Next TopicPython Functions




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf