Question re class variable

jmp jeanmichel at sequans.com
Tue Sep 29 13:11:15 CEST 2015


On 09/29/2015 01:02 PM, jmp wrote:
> class GameObject:
>
>    @property
>    def id(self):
>      return id(self) #use the builtin id function
>
> print GameObject().id
>
> Cheers,
>
> JM

I should add that until you don't serialize your object you're fine.

If you need to serialize it, you may want to look at 
https://docs.python.org/3/library/uuid.html

import uuid

class GameObject:

   def __init__(self):
     self._id = None

   @property
   def id(self):
     if self._id is None:
       # make a UUID based on the host ID and current time
       self._id = uuid.uuid1()
     return self._id



More information about the Python-list mailing list