I had an interview yesterday for Python Developer role. The interviewer asked
- What is size of class and object in Python?
- Where are they stored?
For the second question "Where are they stored?", I answered "In RAM" (Correct me if I am wrong!)
Now, coming to the first question "What is size of class and object in Python?", I did Google search and came across sys.getsizeof()
function.
I ran small test using different classes and their objects. But it was always printing "1056" for classes & "56" for objects .
So, I added inheritance to one of the class and add some more properties & methods to other class. But the result was same.
This might be because they are just storing pointers to respective RAM locations. But then what can be the possible answer for the question interviewer asked me?
Experiment Code:
import sys
class Car:
def __init__(self,carName):
self.carName = carName
def getCarName(self):
return self.carName
class EVCar(Car):
def __init__(self,carName,chargingCapacity):
super().__init__(carName)
self.chargingCapacity = chargingCapacity
def getCharging(self):
return self.chargingCapacity
def getMileage(self):
return self.chargingCapacity//100
class Mobile():
def __init__(self,name,os,battery,price) -> None:
self.name = name
self.os = os
self.battery = battery
self.price = price
def getAllDetails(self):
return "Mobile Name : {} | OS : {} | Battery : {} | Price : {}".format(self.name, self.os, self.battery, self.price)
def getMobileName(self):
return "Mobile Name : {}".format(self.name)
def getMobileOS(self):
return "Mobile OS : {}".format(self.os)
def getMobileBattery(self):
return "Mobile Battery : {}".format(self.battery)
def getMobilePrice(self):
return "Mobile Price : {}".format(self.price)
c = Car('Tata Motors')
print(c.__class__)
print("Class memory :",sys.getsizeof(Car))
print("Object Size using sys.getsizeof() :",sys.getsizeof(c))
print("Object size using __sizeof__() :",c.__sizeof__())
print("--------------------------------------------------------------\n")
e = EVCar('Nexon',120)
print(e.__class__)
print("Class memory :",sys.getsizeof(EVCar))
print("Object Size using sys.getsizeof() :",sys.getsizeof(e))
print("Object size using __sizeof__() :",e.__sizeof__())
print("--------------------------------------------------------------\n")
m = Mobile('Google Pixel', 'Android 12', "5000mAH", "50000/-")
print(m.__class__)
print("Class memory :",sys.getsizeof(Mobile))
print("Object Size using sys.getsizeof() :",sys.getsizeof(m))
print("Object size using __sizeof__() :",m.__sizeof__())
# https://www.google.com/search?q=python+class+occupies+1056+bytes&oq=python+class+occupies+1056+bytes
# https://pymotw.com/3/sys/limits.html (Search for: 1056 on the page)
# https://realpython.com/storing-images-in-python/ (Search for: 1056 on the page)
Output:
<class '__main__.Car'>
Class memory : 1056
Object Size using sys.getsizeof() : 56
Object size using __sizeof__() : 32
--------------------------------------------------------------
<class '__main__.EVCar'>
Class memory : 1056
Object Size using sys.getsizeof() : 56
Object size using __sizeof__() : 32
--------------------------------------------------------------
<class '__main__.Mobile'>
Class memory : 1056
Object Size using sys.getsizeof() : 56
Object size using __sizeof__() : 32