axul
axul

Reputation: 43

Python static method

Object oriented python, I want to create a static method to convert hours, minutes and seconds into seconds.

I create a class called Duration:

    class Duration:
        def __init__(self, hours, minutes, seconds):
            self.hours = hours
            self.minutes = minutes
            self.seconds = seconds

I then create a variable named duration1 where I give it the numbers

duration1 = Duration(29, 7, 10)

I have a method called info which checks if any of the numbers are less than 10, and if so add a "0" in front of it, and then i later revert the values into ints.

def info(self):
    if self.hours < 10:
        self.hours = "0" + str(self.hours)

    elif self.minutes < 10:
        self.minutes = "0" + str(self.minutes)

    elif self.seconds < 10:
        self.seconds = "0" + str(self.seconds)

    info_string = str(self.hours) + "-" + str(self.minutes) + "-" + str(self.seconds)
    self.hours = int(self.hours)
    self.minutes = int(self.minutes)
    self.seconds = int(self.seconds)
    return info_string

And now I want to create a static_method to convert these values into seconds, and return the value so i can call it with duration1 (atleast I think thats how i should do it..?

@staticmethod
def static_method(self):
    hour_to_seconds = self.hours * 3600
    minutes_to_seconds = self.minutes * 60
    converted_to_seconds = hours_to_seconds + minutes_to_seconds \
    + self.seconds
    return converted_to_seconds

duration1.static_method(duration1.info())

I guess my question is how would I use a static method with this code to change the numbers? I should also tell you this is a school assignment so I have to use a static method to solve the problem. Any help is greatly appreciated!

Error message says this:

hour_to_seconds = self.hours * 3600

AttributeError: 'str' object has no attribute 'hours'

Upvotes: 0

Views: 155

Answers (2)

Mike Scotty
Mike Scotty

Reputation: 10782

You're almost there.

Instead of passing the result of duration1.info() to duration1.static_method(), you simply pass the whole object:

duration1 = Duration(29, 7, 10)
print(duration1.info()) # 29-07-10
print(duration1.static_method(duration1)) # 104830

And since it's a static method, you could just as well call it from the class instead of the instance (which is the reason why one wants a static method - you don't have to create an instance):

print(Duration.static_method(duration1))

By the way, as a convention self should not be used as the parameter name if you're using it in a static method, but it works anyway, because - as I said - it's just a convention. It's not as if self is a "magic" variable.

But you should consider to rename the parameter.

By the way, you have a typo in static_method, once you call the variable hour_to_seconds but then access hours_to_seconds - you need to decide on one.

Upvotes: 1

erip
erip

Reputation: 16995

A static method is explicitly a function which is tied to a class rather than an instance. self in Python represents an instance of a class in Python.

These are not simpatico.

You can make your code work by passing duration1 to your static method and changing the name of its accepted parameter from self to duration or similar.

Upvotes: 0

Related Questions