Class N Static
Class N Static
Class N Static
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
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.
print(c1.getCirclesCount()) # -> 3
print(c2.getCirclesCount()) # -> 3
print(Circle.getCirclesCount()) # -> 3
Output of Example 2
Static Method
A method defined inside a class and not bound to either a class or an object is
known as Static Method.
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.
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.
Though existing square function serves the purpose, it is not packaged properly and
does not appear as integral part of class Circle.
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
In Example 2, the square method is defined inside the class Circle and decorated
with staticmethod.
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.
Q1
import os
import sys
import math
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
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
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)))