Calling a method inside a Python class

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 have been doing some practice lately. I came across a question and I wrote the code for it. But I get the error repeatedly: "add_time is not defined". I tried to define the main () method, but then nothing prints. Help.

+3


source to share


4 answers


You have not created an object for the specified class.

Any function / method inside a class can only be accessed by an object of that class. For more information on the basics of object-oriented programming, please check this page .

In the meantime, for this to work, define your class like this:

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, write the following lines:

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

      

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

+3


source


This works fine for me as long as you have specified 3 arguments 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 this 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 their own functions in the class (for example, time_to_int

, int_to_time

or add_time

), you need to retreat from another level of 4 spaces and add self

to your method parameters

+1


source


Hii Mathers25,
I solved your problem by trying this below code to get the best result,

class TimeClass:

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(self,time):

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

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

def add_time(self,t1,t2):
    seconds = self.time_to_int(t1) + self.time_to_int(t2)
    # Call method int_to_time() using self keyword.
    return self.int_to_time(seconds)


# First time object create that time set value is 0 of hour,minute and second
TimeObject = TimeClass(0,0,0)

# After create second object
start=TimeClass(9,45,00)

# After create thired Object
running=TimeClass(1,35,00)

# Store the value which return by add_time() 
done = TimeObject.add_time(start,running)

# Display the value of done variable
print(done)

      

+1


source


Works for me:

class C:
  def f(a, b):
    return a + b
  x = f(1,2)

print(C.x)

      

but you shouldn't do such a thing. Class-level code is executed when the class "creates", usually you need static methods or class methods (decorated with @staticmethod

or @classmethod

) and code execution in some function / instance class. Also you can execute it from above (module) if it's a simple script. Your snippet is "bad practice": the class level (I'm talking about indentation) is for declarations, not for doing something. At the class level, it's okay to execute code that is analogous to C macros: for example, call a decorator, transform some method / attribute / etc - static things that are "pure" functions!

-1


source







All Articles