• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #3397

    Source: Created  with AI tool

    In object-oriented programming, the terms “pure function” and “modifier” (or “mutator”) refer to two different ways methods interact with an object’s state.

    Pure Function:

    A pure function is a method that:
    – Does not modify the state of the object it is called on.
    – Instead, it returns a new value (or object) based on the object’s state and the parameters passed to it.
    – It does not cause side effects. It relies only on the inputs and produces consistent outputs without altering anything external to the method.

    Modifier (or Mutator):

    A modifier is a method that changes the state of the object it is called on. Instead of returning a new object or value, it typically alters the internal attributes (fields) of the object, which can impact the future behavior of the object.

    Explanation of the increment Method:

    Let’s break down what the code is doing:

    # inside class Time:
    
    def increment(self, seconds):
    seconds += self.time_to_int()
    return int_to_time(seconds)
    
    • This method takes a seconds argument and adds it to the current time (converted to seconds using the method time_to_int).
    • It returns a new Time object, which is the result of adding the given number of seconds to the existing time.
    • It does not modify the original time object. The original time stays the same, and instead, a new Time object is created and returned.

    This is why it is considered a pure function—it does not alter the state of the original object; it just computes and returns a new result.

    Modifier Example:

    On the other hand, a modifier method would directly change the state of the object. Here’s an example:

    # inside class Time:
    
    def increment(self, seconds):
    self.seconds += seconds # Directly modify the object's state
    

    In this version, instead of returning a new Time object, the method modifies the current object’s seconds attribute. This makes it a modifier because it changes the state of the object directly.

    Example to Illustrate Pure Function vs Modifier:

    Suppose you have a Time class representing a time of day.

    Pure Function Example:

    class Time:
    def __init__(self, hours, minutes):
    self.hours = hours
    self.minutes = minutes
    
    def add_time(self, minutes):
    # Pure function: Returns a new Time object without modifying the original
    new_minutes = self.minutes + minutes
    new_hours = self.hours + new_minutes // 60
    return Time(new_hours % 24, new_minutes % 60)
    
    # Usage
    time1 = Time(10, 30)
    time2 = time1.add_time(45)
    
    print(time1.hours, time1.minutes) # Outputs: 10 30 (Original object remains unchanged)
    print(time2.hours, time2.minutes) # Outputs: 11 15 (New object is returned)
    

    In this example:
    add_time is a pure function because it does not change time1. It returns a new Time object (time2) without affecting the original.

    Modifier Example:

    class Time:
    def __init__(self, hours, minutes):
    self.hours = hours
    self.minutes = minutes
    
    def increment(self, minutes):
    # Modifier: Changes the state of the object itself
    self.minutes += minutes
    self.hours += self.minutes // 60
    self.minutes = self.minutes % 60
    self.hours = self.hours % 24
    
    # Usage
    time1 = Time(10, 30)
    time1.increment(45)
    
    print(time1.hours, time1.minutes) # Outputs: 11 15 (Object is modified)
    

    In this example:
    increment is a modifier because it changes the internal state of time1 by updating its hours and minutes attributes.

    Summary:

    • A pure function computes and returns a new value or object without changing the original object. It has no side effects.
    • A modifier alters the internal state of the object on which it is called, changing its behavior and attributes going forward.
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Scroll to Top