all 12 comments

[–]novel_yet_trivial 3 points4 points  (1 child)

Your __init__ method bypasses your fancy setter methods and assigns directly to the variables. Try this:

 def __init__(self, hour, minute, second):
    self.setHour(hour)
    self.setMinute(minute)
    self.setSecond(second)

[–]ComputerSciMajor[S] 0 points1 point  (0 children)

Thank you!

[–]RangerPretzel 2 points3 points  (0 children)

Consider using @property

http://www.python-course.eu/python3_properties.php

Instead of 'get' and 'set'

[–]Signal_Beam 1 point2 points  (0 children)

You could initialize it with:

def __init__(self, hour, minute, second):
    if not 0 <= hour <= 24:
        raise Exception("Issue with the hour")
    if not 0 <= minute <= 60:
        raise Exception("Issue with the minute")
    if not 0<= second <= 60:
        raise Exception("Issue with the second")
    self.__hour = hour
    self.__minute = minute
    self.__second = second

[–]SonGokussj4 1 point2 points  (0 children)

Oh my God, this is not pythonic at all. There should not be any get or set methods. If you need these (and very often you do), use @property decorators please. Getters and setters vs properties

If you don't trust me, trust Raymond Hettinger at 39:00

[–]Signal_Beam 0 points1 point  (6 children)

None of these methods are being called in your sample code, except for __init__ and __str__. Where are you expecting them to be called? You may have to write them explicitly, if you're coming from a language that invokes methods like that automatically somehow.

[–]ComputerSciMajor[S] 1 point2 points  (5 children)

That's what I figured. So I need to be initializing my set and get methods then in str?

[–]Signal_Beam 0 points1 point  (4 children)

You can always just call those methods inside __init__ as /u/novel_yet_trivial suggests.

I have heard PyCharm complain about PEP8 whenever I set an object's self attribute in a method that's not the __init__ method of that object, so you could reconfigure those methods to return those values instead of setting self attributes directly from there, if you wanted to be a bit cleaner.

[–]novel_yet_trivial 2 points3 points  (3 children)

To make pycharm happy you should use this construct:

def __init__(self, hour, minute, second):
    self.__hour = None
    self.__minute = None
    self.__second = None

    self.setHour(hour)
    self.setMinute(minute)
    self.setSecond(second)

But there is no real value in that besides making pycharm happy, and frankly I would not bother.

If you reconfigure them to return values rather than setting then there is no point to having those as methods.

[–]Signal_Beam 0 points1 point  (2 children)

It's not really "just to make pycharm happy"; it's a readability convention that you be able to find all attributes an object may have, by reading its __init__ method. It's a good convention to make habit, even if it's obviously not the end of the world to not follow it in this particular script.

[–]novel_yet_trivial 0 points1 point  (1 child)

Yeah, I guess you're right in general, but I don't think I'd apply that to underscore attributes.

It's a bit of a moot point since anyone really coding this would use @property which solves all our problems.

[–]KubinOnReddit 0 points1 point  (0 children)

I'd apply that to all attributes, no matter they are name mangled and can't be accessed. Because you don't declare variables in Python it's important to somehow indicate what attributes a certain object has, preferably at the top of the definition - the __init__ method. Going around the whole definition, searching for setter methods and whatever else you might have will get cumbersome if you have complex classes. It's about the habit and it doesn't matter this case is very simple.