例如:
删除对象:
class MyClass: name = "John"del MyClassprint(MyClass)
1、定义和用法
del
关键字用于删除对象。 在Python中,所有事物都是对象,因此del
关键字也可以用于删除变量,列表或列表的一部分等。
2、使用示例
例如:
删除变量:
x = "hello"del xprint(x)
例如:
删除列表中的第一个元素:
x = ["apple", "banana", "cherry"]del x[0]print(x)
例如:
删除对象:
class MyClass: name = "John"del MyClassprint(MyClass)
del
关键字用于删除对象。 在Python中,所有事物都是对象,因此del
关键字也可以用于删除变量,列表或列表的一部分等。
例如:
删除变量:
x = "hello"del xprint(x)
例如:
删除列表中的第一个元素:
x = ["apple", "banana", "cherry"]del x[0]print(x)