1

My code below works perfectly to receive my input data (list.txt) and return lines between 2 and 5 to the screen.

def get_list():

    file = open('list.txt', 'r')
    lines = file.read().splitlines()[2:5]
    print (lines)

get_list()

However I am required to implement something where I can call the function like:

get_list()[2:5] 

and this would print between lines 2 and 5 instead. Can anyone suggest a possible way I can do this? Thanks!

4
  • Return the whole lines list from your function, then. Commented Oct 10, 2016 at 10:50
  • You'll want to close your file too. You can use with to automatically close the file when you're done with it Commented Oct 10, 2016 at 10:57
  • It's certainly possible, but I'm not convinced that it's actually a sensible thing to do. :) Commented Oct 10, 2016 at 11:01
  • 1
    Do you really mean that get_list()[2:5] should print the selected lines, or do you merely wish it to return those lines (eg in a list or tuple)? Commented Oct 10, 2016 at 11:10

3 Answers 3

1

As I mentioned in the question comments, this is not a sensible thing to do, but here's how it can be done. To test this code I created a text file containing the integers 0 to 9, using the GNU coreutils command seq:

seq 0 9 >list.txt

And here's the Python code:

class AutoPrint(object):
    def __init__(self, data):
        self.data = data

    def __getitem__(self, idx):
        for row in self.data[idx]:
            print(row)

def get_list(fname):
    with open('list.txt') as f:
        data = f.read().splitlines()
    return AutoPrint(data)

get_list('list.txt')[2:5]

output

2
3
4

But please don't do crazy stuff like this in real code! My __getitem__ method returns None; a proper __getitem__ is supposed to return the selected item(s). And it shouldn't have crazy side-effects, like printing stuff. OTOH, I guess while you're developing the code it can be handy for it to print (or log) stuff so that you know that it's doing what you expect it to do.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the detailed answer. I understand this isn't a good thing to do will bear this in mind.
1
def get_list():

   with open('list.txt', 'r') as file:
       lines = file.read().splitlines()
   return lines

print(get_list()[2:5])

3 Comments

@Shaikhar: This code will print all lines....not specified lines ie [2:5]
@mahendrakamble OP needs to call the function and then decide which lines to print.
This does work, I stupidly printed in my function rather than "returned" the line. Thanks.
0

Just return lines !

def get_list():

    file = open('list.txt', 'r')
    lines = file.read().splitlines()
    return lines

1 Comment

That won't print the lines, though.

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.