Can't call static method inside class
Question: This is what i am trying to do call a static method inside a class to populate
the class variable.

    
    
    import sys
    import os
    from HelpingData import *
    
    class Inventory(object):
        shipping_cost = 400.0   
        total_stock = calculate_total_stock.__func__()
    
        def __init__(self, attributes={}):
            self.inventory = {}
            if attributes is None:
                 self.inventory = {}
            else:    
                 for key in attributes:
                    self.inventory[key] = attributes[key]  
    
    
        def getValue(self,attribute):
            return self.inventory[attribute]  
    
    
        def setValue(self,attribute,value):
            self.inventory[attribute]=value 
    
        @staticmethod        
        def calculate_total_stock():
           total_stock = dict((item, 0) for item in product_names)
           for nation in product_stock:
               for item in nation:
                  total_stock[item] += nation[item]
           return total_stock   
    

And this is the error i am getting..

    
    
    >   total_stock = calculate_total_stock.__func__() 
        NameError: name'calculate_total_stock' is not defined
    

Can someone suggest me, what i am missing here, i am new to python. 1 day old


Answer: The code at the top level of the `Inventory` definition (i.e. class attributes
and method definitions) runs _before_ the name `Inventory` exists, so you
can't call its own methods within the definition. As you have a
`@staticmethod`, which doesn't require any class or instance argument, why not
move it outside?

    
    
    def calculate_total_stock(product_names, product_stock):
        total_stock = dict((item, 0) for item in product_names)
        for nation in product_stock:
            for item in nation:
               total_stock[item] += nation[item]
        return total_stock
    
    
    class Inventory(object):
    
        SHIPPING_COST = 400.0   
        TOTAL_STOCK = calculate_total_stock(product_names, product_stock)
    
        def __init__(self, attributes=None):
            self.inventory = {}
            if attributes is not None:
                for key in attributes:
                    self.inventory[key] = attributes[key]  
    
        def get_value(self, attribute):
            return self.inventory[attribute]  
    
        def set_value(self, attribute, value):
            self.inventory[attribute] = value 
    

Note that I have done some tidying up, particularly in terms of
[style](http://legacy.python.org/dev/peps/pep-0008/) and making the explicit
arguments to `calculate_total_stock`.

