Skip to main content
Engineering LibreTexts

17.3: Another example

  • Page ID
    40831
  • Here’s a version of increment (from Section 16.3) rewritten as a method:

    # inside class Time:
    
        def increment(self, seconds):
            seconds += self.time_to_int()
            return int_to_time(seconds)
    

    This version assumes that time_to_int is written as a method. Also, note that it is a pure function, not a modifier.

    Here’s how you would invoke increment:

    >>> start.print_time()
    09:45:00
    >>> end = start.increment(1337)
    >>> end.print_time()
    10:07:17
    

    The subject, start, gets assigned to the first parameter, self. The argument, 1337, gets assigned to the second parameter, seconds.

    This mechanism can be confusing, especially if you make an error. For example, if you invoke increment with two arguments, you get:

    >>> end = start.increment(1337, 460)
    TypeError: increment() takes 2 positional arguments but 3 were given
    

    The error message is initially confusing, because there are only two arguments in parentheses. But the subject is also considered an argument, so all together that’s three.

    By the way, a positional argument is an argument that doesn’t have a parameter name; that is, it is not a keyword argument. In this function call:

    sketch(parrot, cage, dead=True)
    

    parrot and cage are positional, and dead is a keyword argument.

    • Was this article helpful?