Search code examples
pythonfunctionclasscall

How to call a variable defined in one function in another function within same class in python


I have my code as follows -

class utils :
    def __init__(self) :
        self.Name = 'Helen'
        self.count = 0
        self.idcount = 0
        self.date = datetime.datetime.now().strftime("%Y%m%d")

    def getNextId(self) :
        self.idcount += 1
        id = (self.Name+str(self.idcount)+self.date)
        return(id)

    def getCount(self) :
        self.count += 1
        count = str(self.count)
        return(count)

Now I want to use the id and count variable in another function within the same class utils. I tried doing it as follows -

    def formatField(self) :
        self.Id = getNextId().id
        self.cnt = getCount().count
        return(self.cnt+','+self.Id+'            ')

But this doesn't seem to work and gives the error getNextId and getCount are not defined. How to go about it?

Thanks in advance!


Solution

  • self.Id = self.getNextId();
    self.cnt = self.getCount();
    

    But if it's within the same class you can access the member variables directly without using getters.