Is a user-defined class in Python an object itself?

Viewed 38

I have a question to ask you people regarding the behaviour of a class in Python and the scope of a class in Python. Is class itself in Python an object?. I have read some theory about user-defined classes in Python. The theory says that a class in Python itself a class object and that is the reason that we get an id for a user-defined class. If we assume that a class in Python is an object then does a class occupy memory considering that a class is a blueprint?

class Curiousity:
  
   variable="still Curious"
 
print (id(Curiousity))

18234408

1 Answers

[...] then does a class occupy memory considering that a class is a blueprint?

Why not test it yourself, using sys.getsizeof to get the memory size of an object:

import sys


class Curiousity:
    variable = "still Curious"


print(sys.getsizeof(Curiousity))

Out:

1072

Memory footprint of a class can be reduced or managed using a __slots__ attribute:

import sys


class Curiousity:
    __slots__ = ()
    variable = "still Curious"


print(sys.getsizeof(Curiousity))  # now shows as: 904
Related