Rahimi0151
Rahimi0151

Reputation: 411

How can i remove an object in python?

i'm trying to create a chess simulator.

consider this scenario: there is a black rook (instance object of Rook class) in square 2B called rook1. there is a white rook in square 2C called rook2.

when the player moves rook1 to square 2C , the i should remove rook2 object from memory completely.

how can i do it?

P.S. i'v already tried del rook2 , but i don't know why it doesn't work.

Upvotes: 0

Views: 73

Answers (1)

user2357112
user2357112

Reputation: 280310

Trying to remove objects from memory is the wrong way to go. Python offers no option to do that manually, and it would be the wrong operation to perform anyway.

You need to alter whatever data structure represents your chess board so that it represents a game state where there is a black rook at c2 and no piece at b2, rather than a game state where there is a black rook at b2 and a white rook at c2. In a reasonable Python beginner-project implementation of a chess board, this probably means assigning to cells in a list of lists. No objects need to be manually removed from memory to do this.

Having rook1 and rook2 variables referring to your rooks is unnecessary and probably counterproductive.

Upvotes: 4

Related Questions