1

Both versions of the code are working. I am trying to understand the difference between self.data_as_csv vs data_as_csv.

In which scenario each of them is useful over the another.

Version 1:

import pandas as pd   
class test_class:
    def __init__(self, inputFile):
        self.file = inputFile

    def generate_csv(self):
        self.data_as_csv = pd.read_csv(self.file)
        return self.data_as_csv

x = test_class("out.csv")    
df = x.generate_csv()        
print(df)

Version 2:

import pandas as pd   
    class test_class:
        def __init__(self, inputFile):
            self.file = inputFile

        def generate_csv(self):
            data_as_csv = pd.read_csv(self.file)
            return data_as_csv

    x = test_class("out.csv")    
    df = x.generate_csv()        
    print(df)

2 Answers 2

1

using as self, you have access to the variable like this, so if you needed access to the data from the class, it would be useful in that case:

x.data_as_csv

Out[1456]: 
Empty DataFrame
Columns: [P234, Dog, Billy, No, No.1, D32432432, Lost, 01/09/2018, 28/08/2019, return to owner, 123 Fake Drive, LS34 1LE]
Index: []
Sign up to request clarification or add additional context in comments.

Comments

1

using self. will allow you to access to this variable from any method of your class, that means u can have your 'global' variable available only in current class and only for current instance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.