tags:

views:

161

answers:

4

Hello,

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

+6  A: 

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]
RichieHindle
+1 Very clear, thanks
Roee Adler
What about anything outside of the class?
Omega
@Omega: It's the same - just say "module_data = generate_data()" at module scope.
RichieHindle
+3  A: 

The statement data_member = generate_data() will be executed only once, when class coo: ... is executed. In majority of cases class statements occur at module level and are executed when module is imported. So data_member = generate_data() will be executed only once when you import module with class coo for the first time.

All instances of coo class will share data_member and can access it by writing coo.data_member. Any changes made to coo.data_member will be immediately visible by any coo instance. An instance can have its own data_member attribute. This attribute can be set by typing self.data_member = ... and will be visible only to that instance. The "static" data_member can still be accessed by typing coo.data_member.

Jasiu
A: 

You're right. (See this page)

Dario
+5  A: 

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).

dF
+1 several good points, especially about referencing as a class attribute instead of a instance attribute.
D.Shawley
+1 Excellent points, thanks
Roee Adler