with open("text.txt") as inf:
print(inf.read())
User defined classes that implement special methods __enter__() and __exit__() can be used as context managers. More info: http://www.python.org/dev/peps/pep-0343/For just file output/input Scheme has the "with-output-to-file" and "with-input-from-file" macros which redirect output and input to a given file. I vaguely remember Common Lisp having a similar thing, but I can't remember the name off hand.
http://www.lispworks.com/documentation/HyperSpec/Body/m_w_op...
for line in open("name.txt"):
print line # Or do something else with it.:-/
No, it's a code bug.
> Once the iterator finishes, the file should be closed and the file handler freed.
There is no reason for that to happen, let alone for that to happen deterministically.
for line in open(somepath):
# stuff
and f = open(somepath)
for line in f:
# stuff
and the second case would be outright broken if the file just decided to close itself when it stops iterating (as others noted, the developer may now want to append new content, or use file.seek and fly around the file's content).Not to mention a major issue: `break`. The iterator gets no mention of breaks and the iteration is not terminated at this point (as far as the iterator is concerned, that is) so adding a `break` to your iteration (to stop it early because you've found the data/line you needed in a linear search) would suddenly start leaking file handlers where those were collected beforehand... not good.
Which will never happen and really has nothing to do with iteration (or the file object itself)
> I'd love to have this idiom supported under other implementations - it's beautiful.
I disagree on its beauty, but beyond that I can assure you it's never going to be a property of python-the-language.
def in_context(*args) setup_context yield if block_given? ensure teardown_context end
I guess you could build a special class, but why?
IIRC:
{
ifstream file("text.txt");
copy(istream_iterator<string>(file), istream_iterator<string>(),
ostream_iterator<string>(cout, "\n"));
}//file automatically closed here
and class ThingWithFile {
ifstream file;
public:
ThingWithFile(const char *filename) : file(filename) {}
};
void func() {
ThingWithFile thing("file.name"); //file is opened
} //file is closedWhen an object goes out of scope, the destructor should run immediately; then, when the garbage collector wants to actually deallocate destroyed objects, it can go right ahead. No idiocy like in C# with “using” blocks.
If a reference cycle happens then the only good way to detect that the objects involved have been orphaned is to walk the object graph. Doing that every time an object is deallocated would be horribly inefficient. Also, if you're doing it then there's really no need for the reference counting in the first place, since the object graph walk is perfectly capable of finding all the orphan objects on its own. Much better to just do it periodically, so you can get a whole swath of objects each time you walk the graph.
At which point we've come to to garbage collection, and discovered why that's what all the kids are doing these days.
It's a platform where reference counting was already in place, where you can have a reasonable hope that most the programs are too small and simple to take much of a hit on the overhead, and where memory leaks can't get too far out of hand because the OS reserves the right to kill processes whenever it wants.
What overhead? There's no runtime overhead to ARC compared to manual reference counting.
All the garbage collector does is comes through later and deallocates the memory it consumed.
So, in case of C++, object is destroyed when goes out of scope, but the physical act of deallocation may be deferred by the standard library as whatever 'smart memory manager' wishes.
a = new A(); b = new B(); a.b = b; b.a = a;
Whichever you destroy first, the other destructor will have a reference to an unconstructed object.
It isn't ideal, but I think it's an impossible situation - I think predictable destruction is fundamentally at odds with automatic, transparent memory management when you push the ideas to their limits. The idea of automatic resource cleanup is tied closely to the idea of tight (recursive) resource ownership. Automatic memory management exists mostly to take care of the cases when object ownership and lifetime isn't clear.
Reference-counting smart pointers (used properly) can technically give you deterministic destruction, but it's kinda beside the point. We want our file handles and our connections closed as soon as we're done with them, not some time in the future when someone else's code cleans up its references to our old objects.
Folks do still complain that they can't deterministically destroy managed resources such as instances of String. I suspect those folks have missed the plot. That desire is indeed at odds with the memory manager; what is being requested could not (for the most part - the CLR's large object heap does complicate the story a bit) be provided without wholesale downgrading .NET to a less efficient collection algorithm.
It'd be much wiser to instead learn how to implement performance-critical routines that really would benefit from manual memory management in a language that's better suited to that kind of task, such as C. Whining that a garbage collected run-time uses garbage collection is just being silly, not entirely unlike complaining that your horse isn't very good at swimming.
Would suck in a dynamically-typed language, though, because you have no idea whether a reference points to an object with or without a destructor, and so always have to pay the overhead of checking and possibly decrementing the refcount. I don't really see this working without a static type system.
BTW, you're often better off with explicitly-scoped destruction (eg. the Ruby & Python way) than refcounting, for the same reason that boost::shared_ptr can be tricky and Java programs can leak memory. If you don't have clear object ownership & lifetimes, it's very easy to stash a reference to an object in some longer-lived object (a cache, for example) and accidentally hold onto it way longer than anticipated.
It's worse than that. Any object that contains references to other destructible objects (including transitive references), must have an implicit destructor to decrement the refcounts of the destructible objects it points to. In fact, any object that holds a reference, directly or indirectly, to a generic "Object" class or equivalent must have an implicit destructor. This makes a huge part of the object graph implicitly destructible.
If they were deallocated in a piecemeal fashion then:
- Freeing up memory would take longer, because you can't just change a pointer back to the root of the generation.
- Allocating memory would take longer, because it involves searching for a big enough empty slot rather than just throwing the new object on top of the heap.
- More memory is wasted overall, because of heap fragmentation.
But copying is not required for GC. The trusty mark-and-sweep algorithm doesn't copy, for example.
See: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_...
Many others collect non-garbage. Because there typically is way less non-garbage than garbage, that is what makes them competitive with alloc/free.
Going out of scope is deterministic, but the object that has gone out of scope might still be referenced from somewhere if the reference has "escaped". Sometimes automatic escape analysis can work when references escape, but not always.
Would you like the object to have its destructor called even though it might be still be accessible? What would happen if the program subsequently tries to access the object?
Most languages are naturally uncomfortable with tidying up some resoure while it still might referenced. For this reason languages prefer to leave it up to the programmer to explicitly manage destruction themselves.
A perfect example is C#'s IDisposable interface. The language obviously "knows" that a disposable object needs cleaning up after it is used - that's what IDisposable means - but it cannot work out when this should happen. So it lets the programmer decide when that actually happens. If a programmer wants destruction based on scope then she should use the using statement. If she wants destruction based on some other condition then that is possible too. The language cannot make this decision.
I hope that answers your question.
Objects in the stack are easy to use, logical, and orders of magnitude faster to instantiate than objects in the heap!
(when the underlying runtime uses malloc)
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}Ruby has similar concept when you open a file and pass in a block to operate on the File object.
I'm pretty sure the author is just excited about the language and failed to recognize that this pattern is everywhere in different programming languages.
with_under do_something() as f:
...
translated to f = do_something()
...
f.undo_something()
With do_something automatically matched to undo_something. new FileEx (SOME_PATH, () => ConsoleEx.ReadLine (), () => ConsoleEx.CurrentLine != "QUIT").WriteAll ();
is equivalent to using (var file = new File(SOME_PATH))
{
string line = null;
try
{
while((line = Console.ReadLine) != "QUIT")
{
file.WriteLine(line);
}
}
catch (Exception ex)
{
throw;
}
finally
{
sw.Close();
}
}It doesn't seem to be part of the framework though? Homegrown and _you_ expand it to something that is equivalent to the second code snippet?
Another one that I use a lot [2]:
SqlCommandEx sql = "select * from person";
foreach (dynamic result in sql)
{
var full_name = result.first_name + " " + result.last_name;
DateTime dob = result.dob;
//do other stuff
} //connection automatically disposed.
[1] Relevant source: http://pastebin.com/LwHD8MHc[2] https://github.com/noblethrasher/Prelude/blob/master/SQL.cs
1. You're avoiding IDisposable for reasons you don't explain and I fail to see
2. You replace it with an idiom which is supposed to do the same thing (except abusing iterators instead of using tools dedicated to that job) and which I don't believe works, as your connection will not (as far as I can see) be disposed of if there's an error in the foreach block. And the code you linked does not seem to use this "pattern" anywhere
If called from the `foreach` statement, the code in a finally block of an iterator is guaranteed to execute even if the loop exits early due to a `break` or exception. The compiler will also generate a call to Dispose() as well.
Also, it's not really an abuse. When dealing with an unmanaged resource, you're almost always either pulling a stream of data out or pushing a stream of data in. That's precisely the use case of IEnumerable and its dual, IObservable (SqlCommandEx is an example of IEnumerable and FileEx is a loose example of IObservable).
P.S. Now there might be a thus far undiscovered bug in my SqlCommandEx code to which I linked (e.g. the compiler only generates calls to Dispose() on IEnumerator<T> which might not work for dynamic types, I haven't checked) but it's possible to do what I described in principle.
System.IO.File.AppendAllText(path, "This is some text in the file.");
Also, why are you writing text to binary file instead of just writing the text as a text file?String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } }
Anything that implements the AutoCloseable interface gets this behavior for free.
{
open my $fh, '<', "text.txt";
print scalar <$fh>;
}
# $fh goes out of scope so file handle is closed sub with_open_file {
my ($filename, $code) = @_;
open my $fh, '<', $filename or die $!;
$code->($_) for <$fh>;
close $fh;
}
with_open_file 'text.txt' => sub {
my $f = shift;
print $f;
};
And to add to my original answer it's not just the effects of GC that are handy but also arbitrary scoping. Here is an example of what you can do with nested dynamic scoping: our $rake = "in shed";
sub where_is_rake { say $rake }
where_is_rake; # => in shed
{
# take rake out of shed
local $rake = "rake in hand";
where_is_rake; # => rake in hand
{
# pile up leaves
local $rake = "using rake";
where_is_rake; # = using rake
}
where_is_rake; # => rake in hand
}
where_is_rake; # => in shed using(var db = OpenDbConnection())
{
// read from db, connection closed on next curly.
} f = open("test.txt");
scope(exit) f.close();
print(f.read());
The scope(exit) causes the following statement to be executed at the exit of the scope, regardless of how it leaves (return, goto, fall through, throw, etc.).The advantage over try-finally is the 'finally' statement is associated with the open, rather than some arbitrary distance away.
Which is nowhere near as good, as the closing operation has to be setup explicitly. Go has the same issue with `defer`.
http://www.d-programming-language.org/exception-safe.html
Scope guard has a number of distinct advantages.
instead of
You can write The file is automatically closed at the end of the block, and it also handles exceptions and so on. I find it also lines up better semantically with how I think about opening handles to things.I believe Python also added something similar via the 'with' syntax. Not sure about other languages.