0

if i have a python class and it has static attributes and i instantiated two object from the class, will the static attributes be created twice (once for every object) or will it be saved in a static memory that is shared across objects?

I know in C++ there is a type of memory that includes all the static attributes and methods, but it wasn't clear in python if it is the case.

  • Please update your question with the code you have tried when exploring the issue. – quamrana Dec 23 '22 at 18:12
  • this question is not code related it is related to the basic working of the python programming language. – Nicolas Al Ahmar Dec 23 '22 at 18:13
  • I think this answer can give you dome idea: https://stackoverflow.com/a/2923674/13891412 – Clasherkasten Dec 23 '22 at 18:14
  • Do the answers to this [question](https://stackoverflow.com/questions/68645/class-static-variables-and-methods) help at all? – quamrana Dec 23 '22 at 18:17
  • unfortunately no qumrana, but rephrase my question if i had 2 bytes worth of instance attributes and 6 bytes worth of class attributes when i instantiate an object i will reserve 8 bytes, but if i instantiate two object will it reserve 2 new bytes or 8 new bytes? meaning are the class attributes being created again or not? – Nicolas Al Ahmar Dec 23 '22 at 18:32
  • Well, the number of bytes is difficult to determine, but assuming the (class) variables in question are never mutated, then the allocation will take place when the class is defined (and before the first instance is created). Subsequent instance creation will not affect that allocation however many instances are created. (I'm also assuming that what you are calling static attributes are what I think of as class attributes) – quamrana Dec 23 '22 at 18:36
  • thanks this is what i needed to hear, i know the phrasing wasn't exactly right and that python doesn't work this way i was just trying to phrase my question in order to explain it more, thank you again. – Nicolas Al Ahmar Dec 23 '22 at 18:48
  • can you please post this as the answer? – Nicolas Al Ahmar Dec 23 '22 at 22:29

1 Answers1

0

I've come up with some code to explain what happens. The output will show the order in which code is executed.

It will show how the class attributes are created whilst the class is being defined.

(Note that class Report is just a helper here whose only job is to have the side-effect of printing something each time an instance is created)

print('First line.')

class Report:
    def __init__(self, name):
        self.name = name
        print(f'Reporting name:{name}')

    def __str__(self):
        return f'Report named:{self.name}'

print('Before Klass')

class Klass:
    i = Report('i')
    j = Report('j')

    def __init__(self, name):
        self.k = Report(name)

def main():
    print('Starting main()')
    x = Klass('x')
    y = Klass('y')
    print(x.i, x.j)
    print(y.i, y.j)

if __name__ == '__main__':
    print('Above are the "static" allocations')
    print()
    main()
    print('Done')

Output:

First line.
Before Klass
Reporting name:i
Reporting name:j
Above are the "static" allocations

Starting main()
Reporting name:x
Reporting name:y
Report named:i Report named:j
Report named:i Report named:j
Done

Note that if Klass were in a different module and you were to have something like: from Klass import Klass as the first line, then i and j would be printed first.

quamrana
  • 37,849
  • 12
  • 53
  • 71