Classes and Objects
Attributes of an Object
Attributes can be set or accessed using
.
(dot) character.Code
PYTHON
Output
Accessing in Other Methods
We can also access and update properties in other methods.
Code
PYTHON
Output
Updating Attributes
It is recommended to update attributes through methods.
Code
PYTHON
Output
Modeling Class
Let’s model the scenario of shopping cart of ecommerce site.
The features a cart should have
- can add an item
- can remove an item from cart
- update quantity of an item
- to show list of items in cart
- to show total price for the items in the cart
Code
PYTHON