python file API

Chris Angelico rosuav at gmail.com
Mon Sep 24 18:14:01 EDT 2012


On Tue, Sep 25, 2012 at 7:49 AM, Dave Angel <d at davea.name> wrote:
> On 09/24/2012 05:35 PM, zipher wrote:
>> Let file-type have an attribute .pos for position.   Now you can get rid of the seek() and tell() methods and manipulate the file pointer more easily with standard arithmetic operations.
>>
>>>>> file.pos = x0ae1      #move file pointer to an absolute address
>>>>> file.pos +=1            #increment the file pointer one byte
>>>>> curr_pos = file.pos  #read current file pointer
>
> And what approach would you use for positioning relative to
> end-of-file?  That's currently done with an optional second parameter to
> seek() method.

Presumably the same way you reference a list element relative to
end-of-list: negative numbers. However, this starts to feel like magic
rather than attribute assignment - it's like manipulating the DOM in
JavaScript, you set an attribute and stuff happens. Sure it's legal,
but is it right? Also, it makes bounds checking awkward:

file.pos = 42 # Okay, you're at position 42
file.pos -= 10 # That should put you at position 32
foo = file.pos # Presumably foo is the integer 32
file.pos -= 100 # What should this do?
foo -= 100 # But this sets foo to the integer -68
file.pos = foo # And this would set the file pointer 68 bytes from end-of-file.

I don't see it making sense for "file.pos -= 100" to suddenly put you
near the end of the file; it should either cap and put you at position
0, or do what file.seek(-100,1) would do and throw an exception. But
doing the exact same operation on a saved snapshot of the position and
reassigning it would then have quite different semantics in an unusual
case, while still appearing identical in the normal case.

ChrisA



More information about the Python-list mailing list