Python Usage of del with OOP and Objects

Dear StackOverflow community. I have a problem in Python. I created a very basic class named Customer with basic methods to simulate real-life customers. However, I encountered an issue. I defined a class method called remove_customer, and I used the *del *keyword inside it. But I observed that my function does not remove the object belonging to the Customer class. I tried taking out the del keyword and putting outside of class , and I noticed that these two approaches yielded different results. I can’t figure out why this happened. Can someone clarify the situation I encountered?

note:
I have consulted my teacher but he replied me it is a bad practice using OOP and del like this way.

class Customer():
    customer_list=[]
    __slots__=["name","age","gender"]
    def __init__(self,name:str,age:int,gender:int):
        self.name=name
        self.age=age
        self.gender=gender
        Customer.add_customer(self)
    def get_name(self):
        return self.name
    def get_age(self):
        return self.age
    def get_gender(self):
        return self.gender
    @classmethod
    def add_customer(cls,customer):
        cls.customer_list.append(customer)


if __name__=="__main__":
    customer1=Customer("Jack",28,1)
    customer2=Customer("Julia",27,0)
    for x in Customer.customer_list:
        print(x.name)
    Customer.customer_list.remove(customer1)
    del customer1
    print(customer1)
class Customer():
    customer_list=[]
    __slots__=["name","age","gender"]
    def __init__(self,name:str,age:int,gender:int):
        self.name=name
        self.age=age
        self.gender=gender
        Customer.add_customer(self)
    def get_name(self):
        return self.name
    def get_age(self):
        return self.age
    def get_gender(self):
        return self.gender
    @classmethod
    def add_customer(cls,customer):
        cls.customer_list.append(customer)
    @classmethod
    def remove_customer(cls,customer):
        cls.customer_list.remove(customer)
        del customer


if __name__=="__main__":
    customer1=Customer("Jack",28,1)
    customer2=Customer("Julia",27,0)
    for x in Customer.customer_list:
        print(x.name)
    Customer.remove_customer(customer1)
    print(customer1)

first one yields output :

NameError: name ‘customer1’ is not defined. Did you mean: ‘customer2’?

But second one yields different output even doing same thing (I only modified place of del) :

Jack

Julia

<main.Customer object at 0x0000026DD3D76D00>

New contributor

GaussianIntuition is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật