Maxim
Maxim

Reputation: 756

Trouble accessing a variable to use in a class

I'm trying to dynamically set a resource file for a python class I'm building. My idea of tackling this was to initially set the variable to None and then assign it using a module level function. What my module looks like:

# my_module

df = None

def set_data(fp, sheet_name, index_col=[0, 1]):
    # what I really want to do:
    df = pd.read_excel(rf'{fp}', index_col=index_col)

    # minimal example:
    df = 'Foo'

class DataSet():
    def __init__(self):
        self.df = df

Here is the result I get when testing this out. What am I missing here?

>>> import my_module
>>> my_module.df == None
True
>>> my_module.set_data('file.xlsx', 'sheet_1')
>>> my_module.df == None
True
>>> my_module.df == 'Foo'
False

Upvotes: 0

Views: 40

Answers (1)

AChampion
AChampion

Reputation: 30288

A more canonical form would be:

def set_data(fp, sheet_name, index_col=[0, 1]):
    # what I really want to do:
    return pd.read_excel(rf'{fp}', index_col=index_col)

class DataSet():
    def __init__(self, fp, sheet_name, index_col=[0, 1]):
        self.df = set_data(fp, sheet_name, index_col)

Then you would just create your DataSet(), e.g.:

>>> dataset = my_module.DataSet('file.xlsx', 'sheet_1')
>>> dataset.df

As a class variable:

class DataSet():
    df = None

    @classmethod
    def set_data(cls, fp, sheet_name, index_col=[0, 1]):
        cls.df = pd.read_excel(rf'{fp}', index_col=index_col)

Use:

>>> my_module.DataSet.set_data('file.xlsx', 'sheet_1')
>>> dataset = my_module.DataSet()
>>> dataset.df

You can also directly access df from the class without creating an instance:

>>> my_module.DataSet.df

Upvotes: 3

Related Questions