[go: up one dir, main page]

0% found this document useful (0 votes)
111 views6 pages

@classmethod in Python Class

This document discusses class methods in Python. A class method is bound to the class rather than an instance and operates on the class itself. It is defined using the @classmethod decorator and takes the class as the first argument (cls). Class methods can be called without creating an object instance. External functions can also be made class methods by using classmethod(). Class methods can be removed from a class using del or delattr().

Uploaded by

monkey wise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views6 pages

@classmethod in Python Class

This document discusses class methods in Python. A class method is bound to the class rather than an instance and operates on the class itself. It is defined using the @classmethod decorator and takes the class as the first argument (cls). Class methods can be called without creating an object instance. External functions can also be made class methods by using classmethod(). Class methods can be removed from a class using del or delattr().

Uploaded by

monkey wise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2/11/23, 7:56 AM 26.

class methods - Jupyter Notebook

Class Methods @classmethod

In Python, a class method is a method that is bound to the class and not the
instance of the class. This means that it operates on the class itself, rather than
on an instance of the class.
A class method is defined using the @classmethod decorator, followed by a
method that takes the class itself as its first argument, conventionally named cls.
When we go to access instance method data use self key-word, but in class
method we use cls key-word in class method
By using class method you can overload instance method __ init __()
In case of class method, there is no need to create an object of class, we cass
access directly like tes = Test.add()
Class variable can be called without creating class object

In [ ]:

1 ​

In [1]:

1 class Test:
2 a = 20
3 b = 30
4 c = 49
5 @classmethod
6 def add(cls): # cls it mean th
7 return cls.a+cls.b

In [2]:

1 obj = Test()

In [3]:

1 obj.a = 30

In [4]:

1 obj.add()

Out[4]:

50

In [5]:

1 tes = Test.add() # Access class met


2 tes

Out[5]:

50

localhost:8888/notebooks/26. class methods.ipynb 1/6


2/11/23, 7:56 AM 26. class methods - Jupyter Notebook

In [ ]:

1 ​

You can't call method without class object


You can call class method without creating class object

In [6]:

1 class Calculator:
2 def __init__(self, a,b): #ins
3 self.a = a
4 self.b = b
5 def Sub(self): #met
6 return self.a-self.b
7 @classmethod
8 def Sum(cls, a, b): #cla
9 return a+b
10 @classmethod
11 def Mul(cls, a, b): #cla
12 return a*b

In [7]:

1 obj = Calculator(7,8) # Create class o


2 obj.Sub() # calling a meth

Out[7]:

-1

In [8]:

1 Calculator.Sum(2,3) # Calling class me

Out[8]:

In [9]:

1 Calculator.Mul(5,6) # Calling class me

Out[9]:

30

Type Markdown and LaTeX: 𝛼2


Add a external or out sider function as a class method in a class

localhost:8888/notebooks/26. class methods.ipynb 2/6


2/11/23, 7:56 AM 26. class methods - Jupyter Notebook

In [10]:

1 class Company:
2 def __init__(self, product):
3 self.product = product
4 def company_name(self,name):
5 return name
6

In [11]:

1 obj = Company('Laptop')

In [12]:

1 obj.company_name('Lenovo')

Out[12]:

'Lenovo'

In [13]:

1 obj.product

Out[13]:

'Laptop'

In [14]:

1 def product_model(cls, model):


2 print( 'Product model is : ', model)

In [15]:

1 # product_model('Year')

In [16]:

1 Company.product_model = classmethod(product_model) # classmethod

In [17]:

1 Company.product_model('XFGRT-5689')

Product model is : XFGRT-5689

In [ ]:

1 ​

localhost:8888/notebooks/26. class methods.ipynb 3/6


2/11/23, 7:56 AM 26. class methods - Jupyter Notebook

In [ ]:

1 ​

In [18]:

1 def battery_capacity(cls, battery):


2 print('Number of battery is : ', battery)

In [19]:

1 battery_capacity('2 pc') # call function

--------------------------------------------------------
-------------------
TypeError Traceback (mos
t recent call last)
~\AppData\Local\Temp\ipykernel_12336\441683511.py in <mo
dule>
----> 1 battery_capacity('2 pc') # call funct
ion

TypeError: battery_capacity() missing 1 required positio


nal argument: 'battery'

In [20]:

1 Company.battery_capacity = classmethod(battery_capacity)

In [21]:

1 Company.battery_capacity(['GH-45','YU-89'])

Number of battery is : ['GH-45', 'YU-89']

In [ ]:

1 ​

Delete methods from class

del key-word
delattr(class_name, 'class_method') # class method will use as string

In [22]:

1 obj = Company('HP')

localhost:8888/notebooks/26. class methods.ipynb 4/6


2/11/23, 7:56 AM 26. class methods - Jupyter Notebook

In [23]:

1 obj.product

Out[23]:

'HP'

In [24]:

1 Company.product_model('HGTY-5896')

Product model is : HGTY-5896

In [25]:

1 del Company.product_model # delete product_

In [26]:

1 Company.product_model('HGTY-5896') # class method doe

--------------------------------------------------------
-------------------
AttributeError Traceback (mos
t recent call last)
~\AppData\Local\Temp\ipykernel_12336\3874234762.py in <m
odule>
----> 1 Company.product_model('HGTY-5896')
# class method does not exist

AttributeError: type object 'Company' has no attribute


'product_model'

In [ ]:

1 ​

In [27]:

1 Company.battery_capacity('2 PC')

Number of battery is : 2 PC

In [28]:

1 delattr(Company, 'battery_capacity')

localhost:8888/notebooks/26. class methods.ipynb 5/6


2/11/23, 7:56 AM 26. class methods - Jupyter Notebook

In [29]:

1 Company.battery_capacity('2 PC')

--------------------------------------------------------
-------------------
AttributeError Traceback (mos
t recent call last)
~\AppData\Local\Temp\ipykernel_12336\2424357768.py in <m
odule>
----> 1 Company.battery_capacity('2 PC')

AttributeError: type object 'Company' has no attribute


'battery_capacity'

In [ ]:

1 ​

In [30]:

1 from IPython.display import display, HTML


2 display(HTML("<style>.container { width:80% !important; }</style>"

In [ ]:

1 for more, follow : www.linkedin.com/in/imrannazer-

In [ ]:

1 ​

localhost:8888/notebooks/26. class methods.ipynb 6/6

You might also like