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, 1207 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.)
In the first example, We had a list and we wanted to create another list that doesn't affect the main list which you succefully achived in the second example?

Am I right? or I miss something.

Also, please debug first example.
Rs
* Thankful to Allah *
Kurdy
(05-07-2022, 10:05 AM)Mr.Kurd Wrote: In the first example, We had a list and we wanted to create another list that doesn't affect the main list which you succefully achived in the second example?

Am I right? or I miss something.

Also, please debug first example.
Yes you are right but keep in mind the second condition of pure function also that if you run the print statement for first time in first example it will add a to list and if you run it second time this will add another a now the list has two elements. As both the input are same but the output is different.
And degugged the example thanks for it
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,607 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,646 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,826 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,591 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,825 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,775 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,804 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,806 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,801 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)