0

I'll frame this in the fact that I'm kind of a newbie to Python and I'm taking my first stab at writing a class. What I'd like to achieve is for the class to open a text file and return a value from the file based on user input. The text file contains information in a 2D array like this:

 20,21,22
 23,24,25
 26,27,28

The value is retrieved from this text file and assigned to a new variable, so that the variable can be printed and also used later in a calculation. I've been able to do this without difficulty outside of a class, but getting this to work in a class has been frustrating. Here's the code I have so far in Python 2.7:

im_new = []

class Read(object):
    def __init__(self, row, col, newvar):
        self.row = row
        self.col = col
        self.newvar = newvar

        display_list = []
        self.newvar = []

        with open("Array.txt", "r") as data_file:
            for line in data_file:
                display_list.append(line.strip().split(','))

    def __str__(self):
        self.newvar = (display_list[self.row][self.col])
        return self.newvar

immem = Read( 1, 1, im_new)


print "OUTPUT: ", im_new

Ideally, I'd get "OUTPUT: 24", but instead I get "OUTPUT: []". I'm not sure what I'm doing wrong. Any help would be appreciated.

3
  • You're printing im_new, which is initialized as an empty list and never modified. Commented May 5, 2015 at 19:34
  • The first big problem is just that you forgot the self. before display_list. (You've got it on all of your other attributes, so I assume you understand what it means, and this is effectively just a typo?) Commented May 5, 2015 at 19:35
  • The second big problem is that you're printing im_new instead of immem. It's the latter that holds your Read instance, which has a __str__ method, which does what you want. Commented May 5, 2015 at 19:37

1 Answer 1

2

First, you're mixing up two of your variables. im_new is an empty list; immem is a Read instance. Change the last line to print 'OUTPUT:', immem.

When you fix that, you'll get another problem: a NameError on display_list. This is because you forgot the self. on display_list, but in your __init__ method and in your __str__ method. (I assume you know what self. means, because you use it correctly for all of your other attributes.)

As a side note, it's a little weird to override self.newvar every time you get the __str__ of your object. There's really no reason for this attribute to exist, at least in the code you've shown. So you could just do this:

class Read(object):
    def __init__(self, row, col):
        self.row = row
        self.col = col

        self.display_list = []

        with open("Array.txt", "r") as data_file:
            for line in data_file:
                self.display_list.append(line.strip().split(','))

    def __str__(self):
        return self.display_list[self.row][self.col]

immem = Read(1, 1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for walking me through this. I'd been through a number of iterations of this, had gotten the errors you mention, which left me scratching my head. Your explanation makes this clear. Thank you.

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.