Python built-in functions (14) -- delattr, pythondelattr

Source: Internet
Author: User

Python built-in functions (14) -- delattr, pythondelattr

English document:

delattr( Object, Name)
This is a relative setattr(). The arguments are an object and a string. the string must be the name of one of the object's attributes. the function deletes the named attribute, provided the object allows it. for example, delattr(x, 'foobar')Is equivalent del x.foobar.
Note:
  
1. The function is used to delete the attribute of the specified name of the specified object, which is opposite to the setattr function.
# Define class A >>> class A: def _ init _ (self, name): self. name = name def sayHello (self): print ('hello', self. name) # test attributes and Methods>. name 'wheat '>. sayHello () hello wheat # Delete attribute >>> delattr (a, 'name') >>>. nameTraceback (most recent call last): File "<pyshell #47>", line 1, in <module>. nameAttributeError: 'A 'object has no attribute 'name'

2. If the property does not exist, an error is returned.

>>>. Name # The property name has been deleted. Traceback (most recent call last): File "<pyshell #47>", line 1, in <module>. nameAttributeError: 'A' object has no attribute 'name' >>> delattr (A, 'name') # The error Traceback (most recent call last) is returned when you delete the file ): file "<pyshell #48>", line 1, in <module> delattr (a, 'name') AttributeError: name

3. Methods of objects cannot be deleted.

>>>. SayHello <bound method. sayHello of <__ main __. A object at 0x03F014B0 >>>> delattr (a, 'sayhel') # The method Traceback (most recent call last) cannot be deleted: File "<pyshell #50> ", line 1, in <module> delattr (a, 'sayhel') AttributeError: sayHello >>>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.