Allison Wilson
Allison Wilson

Reputation: 250

Python returning memory pointer location rather than value

I am trying to create a python script that appends the current epoc time to the end of a string to create iterative values. The issue, is that IDLE is spitting out the memory pointer position rather than the value and I am not sure why:

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()

Output:

Storm<function dataGen.epochTime at 0x0340A150> Trooper<function dataGen.epochTime at 0x0340A150>

If I take it out of the class and make it a global variable it works:

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()

Output:

Storm1458766044 Trooper1458766044

Upvotes: 0

Views: 6092

Answers (2)

bgporter
bgporter

Reputation: 36494

You're not actually calling the epoch_time function:

def firstName():
        firstName = 'Storm' + str(dataGen.epochTime)
        return firstName

see the difference when you instead do:

def firstName():
        firstName = 'Storm' + str(dataGen.epochTime())
        return firstName

(note the parens after dataGen.epochTime())

It's exactly the same as this:

>>> def f():
...     return 100
...
>>> f
<function f at 0x107d687d0>
>>> f()
100
>>>

Upvotes: 4

Nuno Andr&#233;
Nuno Andr&#233;

Reputation: 5349

Try with:

class dataGen:

    def epochTime(self):
        epoch_time = int(time.time())
        return epoch_time

    def firstName(self):
        firstName = 'Storm' + str(self.epochTime())
        return firstName

    def lastName(self):
        lastName = 'Trooper' + str(self.epochTime())
        return lastName 

def main():
    # Creates an object of dataGen class
    t = dataGen()
    print(t.firstName(), t.lastName())

if __name__ == '__main__':
    main()

Upvotes: 0

Related Questions