How to use a self argument in a function define outside the class. AttributeError: 'function' object has no attribute 'data'

Hi everyone, I have a class “MolecularDataset” which has a self.data argument at line 35 to line 39 as shown in the first pic. I want to access this “self.data” argument in a function “data_noniid” defined outside the class as in the second pic but I have tried both “MolecularDataset.generate.data” and “MolecularDataset.data” but I get an error "AttributeError: ‘function’ object has no attribute ‘data’ ". How can I use the self.data argument in the “data_noniid”.

Based on the pictures, the self.data attribute will be created inside the generate method, so you would need to call it first:

dataset = MolecularDataset()
dataset.generate(args)
print(dataset.data)

Also, note that line34 has a condition to create self.data, which is then used in line37+ without a guard, so it could easily fail.

PS: you can post code snippets by wrapping them into three backticks ```, which would make debugging easier and would allow the search to index your post.

Ok I got what you meant by the args. You meant “filename, add_h, filters, size, validation, test"

Yes, exactly. I’ve used args as a placeholder for the actual input arguments for generate.