Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b314573

Browse files
Understanding about State of functional programming
1 parent 51beb9d commit b314573

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎Classes/state.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
# Have you heard about State in Programming Language?
4+
5+
# Very frequentlt this terminology is been used in Entity or Object Communication.
6+
7+
8+
# Let's say we have to model a bank account with support for deposit and withdraw operations.
9+
# One way to do that is by using global state
10+
11+
balance = 0 #balance variable is global of the program, if this below snippet is inside the class, we can call as class attribute.
12+
13+
def deposit(amount):
14+
global balance
15+
balance += amount
16+
return balance
17+
18+
def withdraw(amount):
19+
global balance
20+
balance -= amount
21+
return balance
22+
23+
sanjay = deposit(100)
24+
print(sanjay)
25+
26+
# The above example is good enough only if we want to have just a single account.
27+
28+
# ******Things start getting complicated if want to model multiple accounts.******
29+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /