IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 6: Python › CS105: Introduction to Python by Saylor Academy › Unit 10: Object-Oriented Programming › Understanding pure functions vs modifiers in object-oriented programming: Concepts and examples
- This topic is empty.
-
AuthorPosts
-
September 6, 2024 at 12:51 pm #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 methodtime_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’sseconds
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 changetime1
. It returns a newTime
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 oftime1
by updating itshours
andminutes
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.
- This method takes a
-
AuthorPosts
- You must be logged in to reply to this topic.