-1
import time
import datetime

class TimeCounter():

    def startTime():

        start_time_of_the_script = time.time()

    def endTime():

        end_time_of_the_script = time.time()
        process_time_in_seconds = end_time_of_the_script - 
start_time_of_the_script
        print(str(datetime.timedelta(seconds=process_time_in_seconds)))

def main():

    TimeCounter.startTime()
    TimeCounter.endTime()

main()

I know that I am doing some fundamental mistake using python functions. Would you mind sharing a solution. Thanks.

questioneer1
  • 5
  • 1
  • 3

2 Answers2

1

You can assign values you want to retrieve later to self. like the following:

import time
import datetime

class TimeCounter:

   def startTime(self):

        self.start_time_of_the_script = time.time()

    def endTime(self):

        self.end_time_of_the_script = time.time()
        self.process_time_in_seconds = self.end_time_of_the_script - self.start_time_of_the_script
        print(str(datetime.timedelta(seconds=self.process_time_in_seconds)))

The self variable explained

Dodge
  • 3,219
  • 3
  • 19
  • 38
  • In the above example we have not written object inside of **class TimeCounter():** while creating the TimeCounter class and the code did work. I wonder in which situations do we have to write **object** inside of class paranthesis? – questioneer1 Sep 05 '18 at 18:37
  • @questioneer1 Yes parentheses are used when a class is subclassing another class. It is not necessary here it seems. I referenced this post: https://stackoverflow.com/questions/4109552/python-class-definition-syntax. It looks like we have both learned something here. I will remove that from my answer. – Dodge Sep 05 '18 at 18:48
  • And here is one more link that explains that further: https://stackoverflow.com/questions/4015417/python-class-inherits-object – Dodge Sep 05 '18 at 18:51
  • Yes very important thing I have learned too. What if I wanted to call say for example the : **process_time_in_seconds** from python shell or from another function later on, what should I do? because it does not simply work like writing **process_time_in_seconds** in the shell as I am using functions in the classes.. – questioneer1 Sep 05 '18 at 19:17
  • You can retrieve class attributes from instances of the class after instantiation. So if your class instance is `tc` you can retrieve said value with `tc.process_time_in_seconds` . – Dodge Sep 05 '18 at 19:23
  • Hmm. for the code most above it says : **NameError: name 'tc' is not defined** when I give **tc.process_time_in_seconds** to the shell – questioneer1 Sep 05 '18 at 19:30
  • yes you would need to instantiate the class with `tc = TimeCounter()` – Dodge Sep 05 '18 at 19:45
  • yes i do it under the **def main():** function already. like @vthorsteinsson given example .. should i do something else? – questioneer1 Sep 05 '18 at 19:57
1

You need to distinguish between classes and instances. Once you have defined TimeCounter as a class, you can create one or many instances of it. This is done below in the assignment tc = TimeCounter(), which creates a fresh instance of TimeCounter and assigns it to the variable tc.

When methods (functions) are called on instances of a class, the instance is passed to the method as a parameter, traditionally called self. So when the code below calls tc.startTime(), the self parameter in startTime will refer to the tc instance. Also, when startTime sets self.start_time_of_the_script, it is creating a new property of the tc instance - which is then read again in endTime.

import time
import datetime

class TimeCounter:  # Parentheses not necessary

    def startTime(self):  # Note self parameter

        self.start_time_of_the_script = time.time()  # Set a property of the instance

    def endTime(self):  # Note self parameter

        end_time_of_the_script = time.time()  # This is a local variable
        process_time_in_seconds = end_time_of_the_script - self.start_time_of_the_script
        print(datetime.timedelta(seconds=process_time_in_seconds))

def main():

    tc = TimeCounter()  # Create a fresh instance of TimeCounter and assign to tc
    tc.startTime()  # Call method startTime on the tc instance
    tc.endTime()

main()
vthorsteinsson
  • 352
  • 2
  • 5
  • Thanks. It did work! However, another question came up as; why we also don't write self.end_time_of_the_script for: end_time_of_the_script and self.process_time_in_seconds for : process_time_in_seconds. why it does't give any error when we don't.? – – questioneer1 Sep 05 '18 at 18:29