0

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'

2
  • 2
    A static method, by definition, is not a method of an instance. Therefore, it cannot have a self parameter. Commented Feb 13, 2018 at 11:02
  • static method doesn't take self parameter as it can be called outside a class object Commented Feb 13, 2018 at 11:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.