Converting Methods to Properties
Properties in Python allow you to access computed values like attributes. This eliminates the need for parentheses when calling a method, making your code cleaner and more consistent.
Currently, our Stock
class has a cost()
method that calculates the total cost of the shares.
def cost(self):
return self.shares * self.price
To get the cost value, we have to call it with parentheses:
s = Stock('GOOG', 100, 490.10)
print(s.cost()) ## Calls the method
We can improve this by converting the cost()
method to a property, allowing us to access the cost value without parentheses:
s = Stock('GOOG', 100, 490.10)
print(s.cost) ## Accesses the property
Instructions:
-
Open the stock.py
file in the editor.
-
Replace the cost()
method with a property using the @property
decorator:
@property
def cost(self):
return self.shares * self.price
-
Save the stock.py
file.
-
Create a new file named test_property.py
in the editor:
touch /home/labex/project/test_property.py
-
Add the following code to the test_property.py
file to create a Stock
instance and access the cost
property:
from stock import Stock
## Create a stock instance
s = Stock('GOOG', 100, 490.10)
## Access cost as a property (no parentheses)
print(f"Stock: {s.name}")
print(f"Shares: {s.shares}")
print(f"Price: {s.price}")
print(f"Cost: {s.cost}") ## Using the property
-
Run the test script:
python /home/labex/project/test_property.py
You should see output similar to:
Stock: GOOG
Shares: 100
Price: 490.1
Cost: 49010.0