1

This is a newbie question, but I looked around and I'm having trouble finding anything specific to this question (perhaps because it's too simple/obvious to others).

So, I am working through Zed Shaw's "Learn Python the Hard Way" and I am on exercise 15. This isn't my first exposure to python, but this time I'm really trying to understand it at a more fundamental level so I can really do something with a programming language for once. I should also warn that I don't have a good background in object oriented programming or fully internalized what objects, classes, etc. etc. are.

Anyway, here is the exercise. The ideas is to understand basic file opening and reading:

from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "I'll also ask you to type it again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

txt.close()
txt_again.close()

My question is, why are the open and read functions used differntly?

For example, to read the example file, why don't/can't I type print read(txt) on line 8?
Why do I put a period in front of the variable and the function after it?
Alternatively, why isn't line 5 written txt = filename.open()?

This is so confusing to me. Is it simply that some functions have one syntax and others another syntax? Or am I not understanding something with respect to how one passes variables to functions.

7
  • 6
    I'd suggest getting another book/reading another tutorial if you're at exercise 15 already and you still this sort of question - looks like the one you have isn't presenting the stuff the right way for you. Commented Apr 1, 2012 at 13:16
  • 2
    @jamylak: What? Java doesn't even have functions.. Commented Apr 1, 2012 at 13:24
  • I meant to generally understand programming at a lower level... Commented Apr 1, 2012 at 13:25
  • 2
    @jamylak: What does this have to do with programming at a lower level? Commented Apr 1, 2012 at 13:29
  • He was confused with things like why he should put a period in front of a variable and the function after it and file steams. Commented Apr 1, 2012 at 13:33

3 Answers 3

4

Syntax

Specifically to the syntactical differences: open() is a function, read() is an object method.

When you call the open() function, it returns an object (first txt, then txt_again).

txt is an object of class file. Objects of class file are defined with the method read(). So, in your code above:

txt = open(filename)

Calls the open() function and assigns an object of class file into txt.

Afterwards, the code:

txt.read()

calls the method read() that is associated with the object txt.

Objects

In this scenario, it's important to understand that objects are defined not only as data entities, but also with built-in actions against those entities.

e.g. A hypothetical object of class car might be defined with methods like start_engine(), stop_engine(), open_doors(), etc.

So as a parallel to your file example above, code for creating and using a car might be:

my_car = create_car(type_of_car)
my_car.start_engine()

(Wikipedia entry on OOP.)

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

Comments

2

To answer this you should have some understanding of object oriented programming.

open() is a normal function, and the first parameter is a string, with the path to the file. The return value of this function is an object.

The further work is done by using this object. An object also has functions, but they are called methods. These methods are called in the context of this object, and the point connects the object with the method. So txt.read() means that you are calling the read-method from the txt-object.

But if you really want to understand this, you should have a look at OOP.

Comments

0

You're coming up against methods vs functions.

open is a global function, and it takes as its parameters simply the things that go between the brackets.

read is a method of file objects. The expression txt.read() calls the read method of the txt object. Under the hood, the txt object is passed as the first parameter of its read method. The read method will be defined something like this:

class File(object):
     def read(self):
         # do whatever here
         # self is whatever object appears to the left of the dot in foo.read

It follows from the above definition that you can only use a method like read on an object which has a read method defined for it.

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.