我正在做cs224n's assignment。在函数test_word2vec
中,有一些我不明白的python语法:
""" Interface to the dataset for negative sampling """
dataset = type('dummy', (), {})()
def dummySampleTokenIdx():
return random.randint(0, 4)
def getRandomContext(C):
tokens = ["a", "b", "c", "d", "e"]
return tokens[random.randint(0,4)], \
[tokens[random.randint(0,4)] for i in xrange(2*C)]
dataset.sampleTokenIdx = dummySampleTokenIdx
dataset.getRandomContext = getRandomContext
问题一:dataset = type('dummy', (), {})()
是什么意思?
问题二:在dataset.sampleTokenIdx = dummySampleTokenIdx
中,我认为dataset
没有属性sampleTokenIdx
。那么,为什么数据集可以调用它呢?
答案 0 :(得分:4)
class dummy(object):
pass