[英]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 使用类NickNames
,需要使用该类生成一些实例,其中id
应该是-增量, nicks
-应该从列表中随机选择,并且timestamp
应采用当前时间戳并将输出存储到列表中
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. 我现在正在得到这种类型的输出[<__main__.NickNames object at 0x00000213547E9AC8>, <__main__.NickNames object at 0x00000213548409E8>]
0x00000213547E9AC8处的[<__main__.NickNames object at 0x00000213547E9AC8>, <__main__.NickNames object at 0x00000213548409E8>]
而不是所需的输出1, crown, 119121392391293 (timestamp)
2, makes, 119121392391298 (timestamp)
请帮助。
Here s the code i
m using 这s the code i
使用s the code i
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)
In the res.append replace uuid.uuid1() by (i + 1). 在res.append中,将uuid.uuid1()替换为(i +1)。 Replace print(res) by: 替换为以下内容:
for obj in res: print(obj.id, obj.nicks, obj.timestamp) 对于res中的obj:print(obj.id,obj.nicks,obj.timestamp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.