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