Class N Static

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Class Methods

A method defined inside a class is bound to its object, by default.

However, if the method is bound to a Class, then it is known as classmethod.

Consider the following two examples:

Example 1 defines the method getCirclesCount, bound to an object of Circle class.

Example 2 defines the classmethod getCirclesCount, bound to class Circle.

Class Methods - Example


Example 1

class Circle(object):
no_of_circles = 0
def __init__(self, radius):
self.__radius = radius
Circle.no_of_circles += 1
def getCirclesCount(self):
return Circle.no_of_circles
c1 = Circle(3.5)
c2 = Circle(5.2)
c3 = Circle(4.8)
print(c1.getCirclesCount()) # -> 3
print(c2.getCirclesCount()) # -> 3
print(Circle.getCirclesCount(c3)) # -> 3
print(Circle.getCirclesCount()) # -> TypeError
Class Methods - Example
Output of Example 1

TypeError: getCirclesCount() missing 1 required positional argument: 'self'

In Example 1, getCirclesCount method is bound to objects of Circle.

Hence when calling it,

on objects c1, and c2 resulted in 3.

on class Circle with object c3 as argument resulted in 3 again.

on class Circle without any object information resulted in TypeError.

Class Methods - Example


Example 2

class Circle(object):
no_of_circles = 0
def __init__(self, radius):
self.__radius = radius
Circle.no_of_circles += 1
@classmethod
def getCirclesCount(self):
return Circle.no_of_circles
c1 = Circle(3.5)
c2 = Circle(5.2)
c3 = Circle(4.8)
In Example 2, getCirclesCount is decorated with classmethod.

Thus making it a class method, which bounds to class Circle.


Class Methods - Example
Example 2...

print(c1.getCirclesCount()) # -> 3

print(c2.getCirclesCount()) # -> 3

print(Circle.getCirclesCount()) # -> 3

Output of Example 2

In Example 2, class Circle is passed as argument to getCirclesCount in both cases.


i.e when it is called on objects and the class.

Static Method
A method defined inside a class and not bound to either a class or an object is
known as Static Method.

Decorating a method using @staticmethod decorator makes it a static method.

Consider the following two examples:

Example1 defines the method square, outside the class definition of Circle, and
uses it inside the class Circle.

Example2 defines the static method square, inside the class Circle, and uses it.

Static Method - Example


Example 1

def square(x):
return x**2
class Circle(object):
def __init__(self, radius):
self.__radius = radius
def area(self):
return 3.14*square(self.__radius)
c1 = Circle(3.9)
print(c1.area())
print(square(10))
Output
47.7594
100
Static Method - Example...
In Example 1, square function is defined outside the class Circle.

It determines square of a number, x.

It can be used outside and also inside the class Circle.

Though existing square function serves the purpose, it is not packaged properly and
does not appear as integral part of class Circle.

Static Method - Example...


Example 2

class Circle(object):
def __init__(self, radius):
self.__radius = radius
@staticmethod
def square(x):
return x**2
def area(self):
return 3.14*self.square(self.__radius)
c1 = Circle(3.9)
print(c1.area())
print(square(10)) # -> NameError
Output

47.7594
NameError: name 'square' is not defined

Static Method - Example...


Example 2...

In Example 2, the square method is defined inside the class Circle and decorated
with staticmethod.

Then it is accessed as self.square.

You can also observe that square method is no longer accessible from outside the
class Circle.

However, it is possible to access the static method using Class or the Object as
shown below.

print(Circle.square(10)) # -> 100

print(c1.square(10)) # -> 100

Q1
import os
import sys
import math

#Add Circle class implementation below


class Circle(object):
no_of_circles = 0
def __init__(self,radius):
self.radius = radius
Circle.no_of_circles += 1
def area(self):
return round((3.14 * self.radius * self.radius),2)
@classmethod
def getCirclesCount(self):
return Circle.no_of_circles
'''Check the Tail section for input/output'''

if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
res_lst = list()
lst = list(map(lambda x: float(x.strip()), input().split(',')))
for radius in lst:
res_lst.append(Circle(radius).area())
fout.write("{}\n{}".format(str(res_lst), str(Circle.no_of_circles)))

Q2
import os
import sys

#Add Circle class implementation here


class Circle:
no_of_circles = 0
def __init__(self,radius):
self.radius = radius
Circle.no_of_circles += 1
def area(self):
return round((3.14 * self.radius * self.radius),2)
@classmethod
def getCircleCount(self):
return Circle.no_of_circles

'''Check the Tail section for input/output'''

if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
res_lst = list()
circcount = list()
lst = list(map(lambda x: float(x.strip()), input().split(',')))
for radi in lst:
c=Circle(radi)
res_lst.append(str(c.getCircleCount())+" : "+str(c.area()))
fout.write("{}\n{}".format(str(res_lst), str(Circle.getCircleCount())))

Q3
import os
import sys

#Add circle class implementation here


class Circle:
no_of_circles = 0
def __init__(self,radius):
self.radius = radius
Circle.no_of_circles += 1
@staticmethod
def getPi():
return 3.14
def area(self):
return round((Circle.getPi() * self.radius * self.radius),2)
@classmethod
def getCircleCount(self):
return Circle.no_of_circles

'''Check the Tail section for input/output'''

if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
res_lst = list()
circcount = list()
lst = list(map(lambda x: float(x.strip()), input().split(',')))
for radi in lst:
c=Circle(radi)
res_lst.append(str(c.getCircleCount())+" : "+str(c.area()))
fout.write("{}".format(str(res_lst)))

You might also like