Question

I have just a simple question. Is there a way to do something like:

(turtle.pos()) = []

so that the turtle's position becomes the title of the list? Or any other way so that a list can be made and the name of it is dependent on the position of the turtle at the time, so that then if the turtle is then back there it can recognize the list linked to that particular location.

Thank you very much. C

Était-ce utile?

La solution

No, you should store the mapping in a dictionary:

positions = {}
positions[tutle.pos()] = []

To check if the turtle ever was in that position, you can do:

stuff = positions.get(tutle.pos())

This will return None if the turtle has never been where it currently is, or the list you put there if the turtle has indeed been there in the past.


Objects in Python don't have "names". There are references that point to an object, but the object itself doesn't depend on the names you give it.

I can create a list:

list1 = []

And give it a few new names:

list2 = list1
list3 = list2

But this doesn't touch the list at all. It just adds new references to it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top