7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 1/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Attributes & Methods
Shopping Cart
Users can add different items to their shopping cart and checkout.
The total value of the cart should be more than a minimum amount (Rs.
100/-) for the checkout.
During Offer Sales, all users get a flat discount on their cart and the
minimum cart value will be Rs. 200/-.
Attributes
Broadly, attributes can be categorized as
Instance Attributes
Class Attributes
Instance Attributes
Attributes whose value can differ for each instance of class are modeled as
instance attributes.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 2/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Ex: Items in Cart
Class Attributes
Attributes whose values stay common for all the objects are modelled as
Class Attributes.
Ex: Minimum Cart Bill,
Flat Discount
Accessing Instance Attributes
Code
PYTHON
1 class Cart:
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 3/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self,..):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(items)
10 a = Cart()
Expand
Output
NameError: name 'items' is not defined
Instance attributes can only be accessed using instance of class.
Self
self passed to method contains the object, which is an instance of class.
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self,item_name, quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self)
10
Expand
Output
<__main__.Cart object at 0x7f6f83c9dfd0> <__main__.Cart object at
0x7f6f83c9dfd0>
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 4/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Accessing Using Self
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self, item_name,quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self.items)
10 a = Cart()
Expand
Output
{"book": 3}
Accessing Using Object
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self, item_name,quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self.items)
10 a = Cart()
Expand
Output
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 5/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
{'book': 3}
Accessing Using Class
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def add_item(self, item_name,quantity):
7 self.items[item_name] = quantity
8 def display_items(self):
9 print(self.items)
10 print(Cart.items)
Output
AttributeError: type object 'Cart' has no attribute
Accessing Class Attributes
Example 1
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6
7 print(Cart.min_bill)
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 6/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Output
100
Example 2
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def __init__(self):
5 self.items = {}
6 def print_min_bill(self):
7 print(Cart.min_bill)
8
9 a = Cart()
10 a.print_min_bill()
Output
100
Updating Class Attribute
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 def print_min_bill(self):
5 print(Cart.min_bill)
6 a = Cart()
7 b = Cart()
8 Cart min bill = 200
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 7/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
8 Cart.min_bill = 200
9 print(a.print_min_bill())
10 print(b.print_min_bill())
Output
200
200
Method
Broadly, methods can be categorized as
Instance Methods
Class Methods
Static Methods
Instance Methods
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 8/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Instance methods can access all attributes of the instance and have self as a
parameter.
Example 1
Code
PYTHON
1 class Cart:
2 def __init__(self):
3 self.items = {}
4 def add_item(self, item_name,quantity):
5 self.items[item_name] = quantity
6 def display_items(self):
7 print(self.items)
8
9 a = Cart()
10 a.add_item("book", 3)
Expand
Output
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50c… 9/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
{'book': 3}
Example 2
Code
PYTHON
1 class Cart:
2 def __init__(self):
3 self.items = {}
4 def add_item(self, item_name,quantity):
5 self.items[item_name] = quantity
6 self.display_items()
7 def display_items(self):
8 print(self.items)
9
10 a = Cart()
Expand
Output
{'book': 3}
Class Methods
Methods which need access to class attributes but not instance attributes
are marked as Class Methods.
For class methods, we send
cls as a parameter indicating we are passing the class.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 10/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 @classmethod
5 def update_flat_discount(cls,
6 new_flat_discount):
7 cls.flat_discount = new_flat_discount
8
9 Cart.update_flat_discount(25)
10 print(Cart.flat_discount)
Output
25
@classmethod decorator marks the method below it as a class method.
We will learn more about decorators in upcoming sessions.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 11/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Accessing Class Method
Code
PYTHON
1 class Cart:
2 flat_discount = 0
3 min_bill = 100
4 @classmethod
5 def update_flat_discount(cls, new_flat_discount):
6 cls.flat_discount = new_flat_discount
7
8 @classmethod
9 def increase_flat_discount(cls, amount):
10 new_flat_discount = cls.flat_discount + amoun
11 cls.update flat discount(new flat discount)
Expand
Output
50
Static Method
We might need some generic methods that don’t need access to either
instance or class attributes. These type of methods are called Static
Methods.
Usually, static methods are used to create utility functions which make more
sense to be part of the class.
@staticmethod decorator marks the method below it as a static method.
We will learn more about decorators in upcoming sessions.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 12/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 class Cart:
2
3 @staticmethod
4 def greet():
5 print("Have a Great Shopping")
6
7 Cart.greet()
Output
Have a Great Shopping
Overview of Instance, Class & Static Methods
Instance Methods Class Methods Static Methods
No cls or self as
self as parameter cls as parameter
parameters
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 13/14
7/27/24, 9:50 PM Revolutionizing the Job Market | NxtWave
Instance Methods Class Methods Static Methods
Need decorator Need decorator
No decorator required
@classmethod @staticmethod
Can be accessed through Can be accessed Can be accessed
object(instance of class) through class through class
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=960d50… 14/14