Zebra
Zebra

Reputation: 139

Python - error with OOP

I have got stuck with Python classes . Please help.

I am getting an error stating that global name 'data' not defined.

Upvotes: 0

Views: 102

Answers (5)

Rohit
Rohit

Reputation: 11

You need to correct this line :

with open(data, 'r') as f:

To :

with open(self.data, 'r') as f:

Upvotes: 1

M.javid
M.javid

Reputation: 6637

In ParseCSV class constructor you set self.data by data parameter, this means that you can access data by self in other methods of the class, change line 6 to below form:

...
    with open(self.data, 'r') as f:
...

Upvotes: 0

Anand S Kumar
Anand S Kumar

Reputation: 90889

You need to reference data variable using the self variable.

Like -

with open(self.data,'r') as f:

Upvotes: 3

Nerkyator
Nerkyator

Reputation: 3976

You have to reference with self.data

Upvotes: 1

James
James

Reputation: 1238

You want to do

def read_data(self):
    with open(self.data, 'r') as f
        ...

Upvotes: 5

Related Questions