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 d968c4d

Browse files
Create Property_decoraters.py (#19)
1 parent 53f9506 commit d968c4d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎OOP/P11_Property decorators.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#This shows the usage of property decorators
2+
3+
#Python @property is one of the built-in decorators. The main purpose of any decorator is to change your class methods or attributes in such a way so that the users neeed not make any additional changes in their code.
4+
5+
#Without property decorators
6+
7+
class BankAccount:
8+
def __init__(self,name,balance):
9+
self.name=name
10+
self.balance=balance
11+
self.total= self.name+ " has "+self.balance+ " dollars in the account"
12+
13+
user1=BankAccount("Elon Musk","10000")
14+
user1.name="Tim cook"
15+
print(user1.name)
16+
print(user1.total)
17+
18+
# Output: Tim cook
19+
# Elon Musk has 10000 dollars in the account
20+
21+
22+
#With property decorators
23+
24+
class BankAccount:
25+
def __init__(self,name,balance):
26+
self.name=name
27+
self.balance=balance
28+
@property
29+
def total(self):
30+
return self.name+ " has "+self.balance+ " dollars in the account"
31+
32+
user1=BankAccount("Elon Musk","10000")
33+
user1.name="Tim cook"
34+
print(user1.name)
35+
print(user1.total)
36+
37+
#Output: Tim cook
38+
# Tim cook has 10000 dollars in the account

0 commit comments

Comments
(0)

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