Search code examples
pythonclassinstancegenerate

Python Generate some instances using predefined class


Using class NickNames, needed to generate some number of instances using this class where id should be - increment, nicks - should be randomly picked from the list and timestamp should take current timestamp and store the output into list

Curently i am getting this type of output [<__main__.NickNames object at 0x00000213547E9AC8>, <__main__.NickNames object at 0x00000213548409E8>] instead of desired 1, crown, 119121392391293 (timestamp) 2, makes, 119121392391298 (timestamp) Please help.

Heres the code im using

import random
import calendar
import time

nicknames = ['child', 'makes', 'override', 'even', 'eleven', 'householder', 'snowball', 'cure', 'crown']

class NickNames:
  def __init__(self, id, nicks, timestamp):
    self.id = id
    self.nicks = nicks
    self.timestamp = timestamp

res = []

for i in range(2):
    res.append(NickNames(uuid.uuid1(), random.choice(nicknames), calendar.timegm(time.gmtime())))

print(res)

Solution

  • In the res.append replace uuid.uuid1() by (i + 1). Replace print(res) by:

    for obj in res: print(obj.id, obj.nicks, obj.timestamp)