我正在尝试创建一个python脚本,将当前epoc时间追加到字符串的末尾以创建迭代值。问题是,IDLE是吐出内存指针位置而不是值,我不确定原因:
class dataGen:
def epochTime(self):
epoch_time = int(time.time())
return epoch_time
def firstName():
firstName = 'Storm' + str(dataGen.epochTime)
return firstName
def lastName():
lastName = 'Trooper' + str(dataGen.epochTime)
return lastName
def main():
t = dataGen.firstName()
t2 = dataGen.lastName()
print(t, t2)
if __name__ == '__main__':
main()
输出:
Storm<function dataGen.epochTime at 0x0340A150> Trooper<function dataGen.epochTime at 0x0340A150>
如果我将它从类中取出并使其成为全局变量,它可以工作:
epoch_time = int(time.time())
class dataGen:
def firstName():
firstName = 'Storm' + str(epoch_time)
return firstName
def lastName():
lastName = 'Trooper' + str(epoch_time)
return lastName
def main():
t = dataGen.firstName()
t2 = dataGen.lastName()
print(t, t2)
if __name__ == '__main__':
main()
输出:
Storm1458766044 Trooper1458766044
答案 0 :(得分:3)
你实际上并没有调用 epoch_time函数:
def firstName():
firstName = 'Storm' + str(dataGen.epochTime)
return firstName
当你做的时候看到差异:
def firstName():
firstName = 'Storm' + str(dataGen.epochTime())
return firstName
(注意dataGen.epochTime()
之后的parens)
与此完全相同:
>>> def f():
... return 100
...
>>> f
<function f at 0x107d687d0>
>>> f()
100
>>>
答案 1 :(得分:0)
尝试:
+