0

I'm trying to understand when you would want to have an instance of a class, and what exactly the difference is between these two variations of code:

Class A takes a time and and assigns it to a new variable, and then returns that new variable.

class A:
    def B(time):
        seconds = time
        return seconds
seconds = A.B(int)

Class C takes a time in as well, but also creates an instance of function D (using self) and then returns self.seconds.

class C:
    def D(self, time):
        self.seconds = time
        return self.seconds

seconds = C().D(int)

They end up returning the same values. I'm have difficulty understanding how these two pieces of code are different. Is one superior in certain situations vs. the other?

Thank you!

EDIT: Added calls to both functions.

TwoPointOH
  • 41
  • 3
  • 7

2 Answers2

0

Perhaps the following (very simplified) example helps to see where it can be useful to have an instance of a class:

class A:
    minutes = 2
    def time(seconds):
        return 60*A.minutes + seconds

class B:
    minutes = 2
    def time(self, seconds):
        return 60*self.minutes + seconds

print("Class:")
print(A.time(30))
a1 = A
a2 = A
a1.minutes = 3
a2.minutes = 4
print(a1.time(30))   # 60*4 + 30
print(a2.time(30))   # 60*4 + 30
print("Instance:")
print(B().time(30))
b1 = B()
b2 = B()
b1.minutes = 3
b2.minutes = 4
print(b1.time(30))   # 60*3 + 30
print(b2.time(30))   # 60*4 + 30

This results in:

Class:
150
270
270
Instance:
150
210
270
0

At the core, what you are asking for is understanding the difference between a Static Method and a normal Method, and what the advantages of OOP are.

A Static method can be called without instantiating the class, such as in your first example. This allows you to group related functions under a single header (the class), but it is not, technically, OOP.

However, a Static method has no instance data to act upon, as there is no instance.

A class instance allows you to keep track of Object specific data, and act upon them within your methods.

For example:

import time

class Stopwatch:
    def __init__(self, name, start):
        self.name = name
        self.time = start
        self.active = False
    def start(self):
        self.active = True
    def stop(self):
        self.active = False
    def reset(self, time):
        self.time = time
    def isComplete(self):
        return (self.time == 0)
    def tick(self):
        if self.active:
            self.time -= 1
            print(self.name+": ",self.time)
            if self.time == 0:
                print("Timer of " + self.name + " has reached zero!")
                self.stop()

watchA = Stopwatch("A", 10)
watchB = Stopwatch("B", 5)

watchA.start()
watchB.start()

while (not watchA.isComplete()) or (not watchB.isComplete()):
    time.sleep(1)
    watchA.tick()
    watchB.tick()

Running this outputs:

A:  9
B:  4
A:  8
B:  3
A:  7
B:  2
A:  6
B:  1
A:  5
B:  0
Timer of B has reached zero!
A:  4
A:  3
A:  2
A:  1
A:  0
Timer of A has reached zero!

Notice how the time of each watch is tracked separately, despite using the same code. This would not be possible using Static methods, as the data is attached not to the Class, but to the Instanced objects themselves.

Kal Zekdor
  • 1,172
  • 2
  • 13
  • 23