[Tutor] is this use or abuse of __getitem__ ?
fomcl at yahoo.com
Sat Sep 15 16:18:26 CEST 2012
>On Sat, Sep 15, 2012 at 4:43 AM, eryksun <eryksun at gmail.com> wrote:
>> start = index(self.nCases + key if key < 0 else key) # may
>> raise TypeError
>> stop = start + 1
>> step = 1
>Gmail is such a pain sometimes. I should have called index first anyway:
> key = index(key) # may raise TypeError
> start = key + self.nCases if key < 0 else key
> stop = start + 1
> step = 1
Thanks, I hadn't noticed this yet. I am refactoring some of the rest of my code and I hadn't run anything yet. My code has two methods that return record(s): an iterator (__getitem__) and a generator (readFile, which is also called by __enter__). Shouldn't I also take the possibility of a MemoryError into account when the caller does something like data[:10**8]? It may no longer fit into memory, esp. when the dataset is also wide.
>> records = 
>> for i in range(start, stop, step):
>You can boost the performance here a bit by caching the append method.
>This avoids a LOAD_ATTR operation on each iteration:
> records = 
> append = records.append
> for i in range(start, stop, step):
I knew that trick from http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avoiding_dots... but I didn't know about LOAD_ATTR. Is a list comprehension still faster than this? Does it also mean that e.g. "from ctypes import *" (--> c_long()) is faster than "import ctypes" (--> ctypes.c_long()). I am now putting as much as possible in __init__. I don't like the first way of importing at all.
More information about the Tutor