Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.5: Pure Functions

2 Replies, 1231 Views

[Image: BFMMlbcQvml9HSqXcvNp]
Pure Functions:
           In the previous thread we started a new concept called functional programming and we discussed it deeply and now we are going to see some other concepts inside the functional programming. In the previous thread we talked about something i.e Pure functions. So  in this thread we are going to look at this concept. And how these are beneficial and why we should use this over other functions.
Any function can be pure functions but it needs to meet two requirements:



Quote:first one is for the same input output is always the same.


It is same as functions in mathematics for example we have a function  f(x) = x2+ x . So has you can see if you say x = 2 for that value the output is always 6 it will never change this is really the same for functions in programming. 



Quote:And the second one is that the function should not have any side effects.

In simple terms, the side effect means the function should not modify the state of 

the variable globally. Look at this example to get a better understanding of this concept of side effects.

Code:
list = []

def alphabets(bar):
    list.append(bar)

alphabets('a')
print(list)


In this example the function alphabet is changing the state of the list which is outside the function is changing the state of the list. Now let’s see how we can change this function to a pure function.

Code:
def alphabets(bar, lst):
    return lst + [bar]

list = []
list_now = alphabets('a', list)

print (list)

Quote:Exclamation   Note : Print function is used to see the effect of both functions


      In impure every time even if we give the same input the output is different and also has the side effects but in impure functions the output is the same every time for the same input. And it is not affecting the list which is a global variable.
The pure functions have greater advantages over impure ones.
These are easy to debug because as it is not have any side effects so if the problem occurs you will know that fault is inside the function so it will make program easy to debug in this sense
The second advantage of pure function is that, The Pure functions don't read or write to any shared memory, and therefore can run in separate threads without any unexpected consequence

So that’s it for now.




"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
"Keep Learning and Keep exploring"
Love me like you do Heart
(This post was last modified: 05-07-2022, 10:33 AM by Covid-19.)

Messages In This Thread
Python 2.5: Pure Functions - by Covid-19 - 05-05-2022, 05:04 PM
RE: Python 2.5: Pure Functions - by Mr.Kurd - 05-07-2022, 10:05 AM
RE: Python 2.5: Pure Functions - by Covid-19 - 05-07-2022, 10:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,264 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,666 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,701 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,865 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,625 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,862 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,814 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,863 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,845 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,855 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)