I'm trying to implement a function that will delete the instance of the class created after a certain period of time, however I cannot seem to implement such a method Have tried finding the time elapsed from the time an instance of a class is created to the current time but that doesnt seem to go very well, any suggestions will be very helpful!
Asked
Active
Viewed 749 times
-1
-
What's your actual goal though that you want to achieve by deleting an instance at a certain time? This is an odd requirement, and you can't guarantee that an instance will be deleted anyway. – Carcigenicate Jun 05 '20 at 14:01
-
I'm planning on making a game that spawns circles at random locations, if you click it, you get points for doing so, if you don't (well you gotta lose somehow) the opportunity to click that circle to get points disappears and you lose a life in the process – awesomelej Jun 05 '20 at 14:08
-
1Don't try to hinge that on deleting an instance. I'm sure Pygame has some kind of timer mechanism, and a way to "destroy" objects within the context of the game. When you create a circle, start a timer, and have the timer call a "destroy" Method on the object after a period of time. – Carcigenicate Jun 05 '20 at 14:10
2 Answers
1
I think thats already two questions -
1) how to “kill” a class instance/object at all ?
- this is covered already here nicely : Python: how to "kill" a class instance/object?
2) how to make an expiring function.
- You might look into the python module "expiringdict" - but there are many other cache implementations with expiring functions. for the beginning, You might look here : https://github.com/mailgun/expiringdict
I did not manage to format Your second question in the comments -
I am not aware that the creation time is stored along with the created instance, so You need to do that Yoursef :
# STDLIB Imports
import time
Class Circle(object):
def __init__(self, diameter, x_coordinate, y_coordinate):
self.creation_time = time.time()
...
my_circle = Circle(1,0,0)
time.sleep(1)
print('creation time: {}'.format(my_circle.creation_time))
age = time.time() - my_circle.creation_time
print('age: {}'.format(age))

bitranox
- 1,664
- 13
- 21
-
Thanks for the suggestions! Really appreciate them, just a few questions, how do i implement method 1, i.e how do I find the time elapsed between the time when a new instance of a Circle is created and the current time? And also do I put this as a class method or like just a general function – awesomelej Jun 05 '20 at 14:20
-
well, I dont know Your code. I am not aware that the creation time is stored somewere along with the created object - so You need to do that Yourself. ` Class Circle(object):` ` def__init__(self, diameter, x_coordinate, y_coordinate, colour):` ` self.creation_time = time.time()` ` ...` – bitranox Jun 05 '20 at 14:27
-
1
Make sure you have imported the time
module.
- In your
__init__()
function for your class, put the value oftime.time()
into a variable, likeself.spawned_time
. - Make a method in your class called
can_destroy()
. The function's code will be as follows:return time.time() >= self.spawned_time + <INSERT TIME THE CLASS WILL BE ALIVE>
Full code:
import time
class MyClass:
def __init__(self):
self.spawned_time = time.time()
def can_destroy(self):
return time.time() > self.spawned_time + 6 # replace '6' with the seconds the class will exist for
my_instance = MyClass()
while True:
if my_instance.can_destroy():
# destroy your instance here

programmerpremium
- 224
- 1
- 9