Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling a method inside a class-Python

class Time:

    def __init__(self,x,y,z):
        self.hour=x
        self.minute=y
        self.second=z

    def __str__(self):
        return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)

    def time_to_int(time):
        minutes=time.hour*60+time.minute
        seconds=minutes*60+time.second
        return seconds

    def int_to_time(seconds):
        time=Time()
        minutes,time.second=divmod(seconds,60)
        time.hour,time.minute=divmod(minutes,60)
        return time

    def add_time(t1,t2):
        seconds=time_to_int(t1)+time_to_int(t2)
        return int_to_time(seconds)

start=Time(9,45,00)
running=Time(1,35,00)
done=add_time(start,running)
print(done)

I am new to python and i've been doing some practice lately.I came across a question and i've written the code for the same.But I am repeatedly getting an error: "add_time is not defined". I tried defining a main() method but then it doesn't print anything.Please help.

like image 439
mathers25 Avatar asked Jun 01 '17 07:06

mathers25


2 Answers

You haven't created an object to the above class.

Any function/method inside a class can only be accessed by an object of that class .For more information on the fundamentals of Object Oriented Programming, please check this page.

Meanwhile for this to work, define your class in the following way :

class Time:

def __init__(self,x=None,y=None,z=None):
    self.hour=x
    self.minute=y
    self.second=z

def __str__(self):
    return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)

def time_to_int(time):
    minutes=time.hour*60+time.minute
    seconds=minutes*60+time.second
    return seconds

def int_to_time(seconds):
    time=Time()
    minutes,time.second=divmod(seconds,60)
    time.hour,time.minute=divmod(minutes,60)
    return time

def add_time(t1,t2):
    seconds=time_to_int(t1)+time_to_int(t2)
    return int_to_time(seconds)

and outside the class block, write the following lines :

TimeObject = Time()
start=Time(9,45,00)
running=Time(1,35,00)
TimeObject.add_time(start,running)
print "done"

I however suggest you to write the add_time function outside the class because you are passing the objects to the class as the parameters to the function within the same class and it is considered as a bad design in object oriented programming. Hope it helps. Cheers!

like image 136
crazyglasses Avatar answered Sep 28 '22 07:09

crazyglasses


This works fine for me as long as you specified 3 args in your constructor

def int_to_time(seconds):
    time=Time(0,0,0) # just set your 3 positionals args here
    minutes,time.second=divmod(seconds,60)
    time.hour,time.minute=divmod(minutes,60)
    return time

Another way to avoid it could be:

class Time:
    def __init__(self,x=0,y=0,z=0):
        self.hour=x
        self.minute=y
        self.second=z

If you want to add your functions to your class (such as time_to_int, int_to_time or even add_time) then you will need to indent with one more level of 4 spaces and add self to your method parameters

like image 34
pBouillon Avatar answered Sep 28 '22 07:09

pBouillon