1

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

Artier
  • 1,648
  • 2
  • 8
  • 22

1 Answers1

1

[...] 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
Kraigolas
  • 5,121
  • 3
  • 12
  • 37
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53