Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic Way to Initialize (Complex) Static Data Members

Tags:

python

class

I have a class with a complex data member that I want to keep "static". I want to initialize it once, using a function. How Pythonic is something like this:

def generate_data():
    ... do some analysis and return complex object e.g. list ...

class Coo:
    data_member = generate_data()
    ... rest of class code ...

The function generate_data takes a long while to complete and returns data that remains constant in the scope of a running program. I don't want it to run every time class Coo is instantiated.

Also, to verify, as long as I don't assign anything to data_member in __init__, it will remain "static"? What if a method in Coo appends some value to data_member (assuming it's a list) - will this addition be available to the rest of the instances?

Thanks

like image 692
Roee Adler Avatar asked May 16 '09 18:05

Roee Adler


People also ask

How do you initialize a static object in C++?

As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).

How do you initialize a static variable in Python?

There are the two ways to define a static method in Python: Using the staticmethod() Method. Using the @staticmethod Decorator.

Can constructor be static in Python?

As soon as the interpreter parses and starts executing all of those classes and def statements, the equivalent of a static constructor is being run. The class definitions are being executed at that point. You can put any number of statements anywhere inside the class body and they are in effect a static constructor.

What is static block in Python?

Static methods belong to the class and they will be loaded into the memory along with the class, you can invoke them without creating an object. (using the class name as reference). Whereas a static block is a block of code with a static keyword. In general, these are used to initialize the static members.


2 Answers

You're right on all counts. data_member will be created once, and will be available to all instances of coo. If any instance modifies it, that modification will be visible to all other instances.

Here's an example that demonstrates all this, with its output shown at the end:

def generate_data():
    print "Generating"
    return [1,2,3]

class coo:
    data_member = generate_data()
    def modify(self):
        self.data_member.append(4)

    def display(self):
        print self.data_member

x = coo()
y = coo()
y.modify()
x.display()

# Output:
# Generating
# [1, 2, 3, 4]
like image 146
RichieHindle Avatar answered Sep 24 '22 18:09

RichieHindle


As others have answered you're right -- I'll add one more thing to be aware of: If an instance modifies the object coo.data_member itself, for example

self.data_member.append('foo')

then the modification is seen by the rest of the instances. However if you do

self.data_member = new_object

then a new instance member is created which overrides the class member and is only visible to that instance, not the others. The difference is not always easy to spot, for example self.data_member += 'foo' vs. self.data_member = self.data_member + 'foo'.

To avoid this you probably should always refer to the object as coo.data_member (not through self).

like image 21
dF. Avatar answered Sep 25 '22 18:09

dF.