
Delete attributes from an instance in Python
del
, a Python built-in function, deletes the instance's property.
class Account:
def __init__(self, name):
self.name = name
@staticmethod
def agree():
print('I agree.')
Account.agree() # I agree.
a = Account(name='Kyle')
del a.name
print(a.name)
# AttributeError: 'Account' object has no attribute 'name'
After deleting name
property, we can't access a.name
but assign a value to it.
class Account:
def __init__(self, name):
self.name = name
@staticmethod
def agree():
print('I agree.')
Account.agree() # I agree.
a = Account(name='Kyle')
del a.name
a.name = 'Stan'
print(a.name) # Stan
Comments
Powered by Markdown