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 916d34f

Browse files
authored
Create Accessing_pvt_var1.py
Description in Docstring of code
1 parent a03134c commit 916d34f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎docs/OOPS/Accessing_pvt_var1.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
When we put a double underscore in front of the attribute name, python will internally change its name to _Classname__attribute.
3+
4+
Since we know that the name of the variable changes when we make it private, we can access it using its modified name as shown below:
5+
'''
6+
class Customer:
7+
def __init__(self, cust_id, name, age, wallet_balance):
8+
self.cust_id = cust_id
9+
self.name = name
10+
self.age = age
11+
self.__wallet_balance = wallet_balance
12+
13+
def update_balance(self, amount):
14+
if amount < 1000 and amount > 0:
15+
self.__wallet_balance += amount
16+
17+
def show_balance(self):
18+
print ("The balance is ",self.__wallet_balance)
19+
20+
c1=Customer(100, "Gopal", 24, 1000)
21+
c1._Customer__wallet_balance = 10000000000
22+
c1.show_balance()

0 commit comments

Comments
(0)

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