Do I need to call a class everytime to use its functions?

class Dog():
dog_time_dilation = 7

def time_explanation(self):
print(“Dogs experience {} years for every 1 human year.”.format(self.dog_time_dilation))

pipi_pitbull = Dog()
pipi_pitbull.time_explanation()

Prints “Dogs experience 7 years for every 1 human year.”


I have a question, this works fine, but my question is this. Do I have to call the class every time I want to use the function is in that class. i.e. it seems here we opened the class then used the variable from the class when we called the function in that class. How many times can I call a function based on my original call of the class?

on the main, when i write a function, the global variable is exists in this function.
is it beacuse the method get , the “self”
exactly like in a class?

im not sure whats you mean, iam doing this class right now so dont trust me; but from my understanding:
lets say you created a string
a=“hello”
so its create an object, from the class
so ofcourse you can use now any method in the class
with a.the_method_you_want

I am pretty sure I am asking the question wrong. I think when you call a method from the class you use the ‘pipi_pitbull.time_explanation()’ to get at the method there, but do you have to call the class only once in your program to get the dog_time_dilation (object variable) and is that object now always available for me to use, since I have to init the class?

I am still trying to wrap my head around this. But as I am going through the lessons, I kinda get it, I think, well maybe.

You wouldn’t call a class to get at the things it defines. Calling a class creates an instance. You might then want to ask an instance to do something.

You could create counters (google mechanical counter)
and stick them in a list:

class Counter:
    def __init__(self):
        self.n = 0

    def increment(self):
        self.n += 1

    def read(self):
        return self.n


number = 234728957
digitCounts = [Counter() for _ in range(10)]
for digit in str(number):
    digit = int(digit)
    digitCounts[digit].increment()


print([c.read() for c in digitCounts])

You could then ask individual counters to increment, and then at some later time maybe you’d like to read the current count of that particular counter.

(and obviously, a plain integer is a better fit for this particular problem, the class isn’t helping in any way)