Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:45:52+00:00 2026-05-30T08:45:52+00:00

I’m learning python useing How to think like a computer scientist: Learn python 3.

  • 0

I’m learning python useing How to think like a computer scientist: Learn python 3.

I’m learning OOP and have put together some code to answer a question in the book, but I feel like I should have done something else.

The code in question is incremental(), which the goal of is to increase the object’s values. Now my final solution was to make my method a copy of the initializer method and just add on the time right there.

This feels sloppy:

class MyTime:

    def __init__(self, hrs=0, mins=0, secs=0,):
       """ Create a new MyTime object initialized to hrs, mins, secs.
           The values of mins and secs may be outside the range 0-59,
           but the resulting MyTime object will be normalized.
       """

       # calculate total seconds to represent
       totalsecs = hrs*3600 + mins*60 + secs
       self.hours = totalsecs // 3600        # split in h, m, s
       leftoversecs = totalsecs % 3600
       self.minutes = leftoversecs // 60
       self.seconds = leftoversecs % 60

    def incerment(self,t):
       # increase the time by t amount
       totalsecs = self.hours * 3600 + self.minutes * 60 + self.seconds + t
       self.hours = totalsecs // 3600        # split in h, m, s
       leftoversecs = totalsecs % 3600
       self.minutes = leftoversecs // 60
       self.seconds = leftoversecs % 60


t1 = MyTime(5,5,5)
t2 = MyTime(10,10,10)
t3 = MyTime(12,12,12)

print('before:',t1)
t1.incerment(100)
print('after:',t1)

So how about it?
Is there a way to clean this up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:45:54+00:00Added an answer on May 30, 2026 at 8:45 am

    That feeling like you should have done something else it’s beacuse hours, minutes, seconds are properties.

    You don’t really need those values stored as attributes of your object, you just want to be able to access those values when you need.

    Calling something like:

    >>> t1.hours
    5
    

    So, let’s rewrite your example using property:

    class MyTime:
        def __init__(self, hrs=0, mins=0, secs=0):
           self.totalsecs = hrs*3600 + mins*60 + secs
    
        @property
        def hours(self):
           return self.totalsecs // 3600
    
        @property
        def minutes(self):
            return self._get_leftoversecs() // 60
    
        @property
        def seconds(self):
            return self._get_leftoversecs() % 60
    
        def _get_leftoversecs(self):
            return self.totalsecs % 3600
    
        def increment(self, t):
            self.totalsecs += t
    

    Usage example:

    >>> t1 = MyTime(5,5,5)
    >>> t1.hours
    5
    >>> t1.hours()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not callable
    >>> t1.seconds
    5
    >>> t1.totalsecs
    18305
    >>> t1.increment(10)
    >>> t1.seconds
    15
    >>> t1.totalsecs
    18315
    

    I don’t know if you noticed but you don’t actually need the increment function anymore:

    >>> t1.totalsecs += 10
    >>> t1.totalsecs
    18325
    

    I know that property must be a little ahead of what you’re doing, but I thought it would have been worth an example.

    Edit: As Lattyware noticed there’s no need to make totalsecs a property too.

    To quote his comment: The great thing about Python’s properties is you don’t need to turn everything into getters and setters to keep a consistent interface like you do in some languages.

    There might be an advantage in setting totalsecs as a property (read-only) only if for some reason you want to hide the internal implementation of MyTime (obviously reintegrating the increment() method).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.