Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python open() vs. .close()

Tags:

python

Regarding syntax in Python

Why do we use open("file") to open but not "file".close() to close it?
Why isn't it "file".open() or inversely close("file")?

like image 896
markiv189 Avatar asked Dec 06 '16 17:12

markiv189


People also ask

How is file open () function different from close () function?

How is file open() function different from close() function? open() as it name implies used to open a data file in a program through file object and close() is used to close() the link of file object and data file. Once data file is closed, no operation can be performed.

What does close () do in Python?

The close() method closes an open file.

What does open () do in Python?

Python open() Function The open() function opens a file, and returns it as a file object. Read more about file handling in our chapters about File Handling.

Is close () necessary?

Python File close() Method Calling close() more than once is allowed. Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.


1 Answers

It's because open() is a function, and .close() is an object method. "file".open() doesn't make sense, because you're implying that the open() function is actually a class or instance method of the string "file". Not all strings are valid files or devices to be opened, so how the interpreter is supposed to handle "not a file-like device".open() would be ambiguous. We don't use "file".close() for the same reason.

close("file") would require a lookup of the file name, then another lookup to see if there are file handles, owned by the current process, attached to that file. That would be very inefficient and probably has hidden pitfalls that would make it unreliable (for example, what if it's not a file, but a TTY device instead?). It's much faster and simpler to just keep a reference to the opened file or device and close the file through that reference (also called a handle).

Many languages take this approach:

f = open("file") # open a file and return a file object or handle
# stuff...
close(f)         # close the file, using the file handle or object as a reference

This looks similar to your close("file") construct, but don't be fooled: it's closing the file through a direct reference to it, not the file name as stored in a string.

The Python developers have chosen to do the same thing, but it looks different because they have implemented it with an object-oriented approach instead. Part of the reason for this is that Python file objects have a lot of methods available to them, such as read(), flush(), seek(), etc. If we used close(f), then we would have to either change all of the rest of the file object methods to functions, or let it be one random function that behaves differently from the rest for no good reason.

TL;DR
The design of open() and file.close() is consistent with OOP principals and good file reference practices. open() is a factory-like function that creates objects that reference files or other devices. Once the object is created, then all other operations on that object are done through class or instance methods.

like image 119
skrrgwasme Avatar answered Oct 10 '22 07:10

skrrgwasme