[Pyrex] __getitem__ vs __getattr__?

Francesc Alted falted at openlc.org
Mon May 26 13:12:52 CEST 2003


A Dissabte 24 Maig 2003 02:48, Joseph Koshy va escriure:
> Hi List,
>
> This is a style issue perhaps: what are the pros and cons of making
> an extension type act like a dictionary (via __getitem__) and
> appearing to have 'attributes' (via __getattr__)?

I too think that this is largely a question of style unless you want to see
your access going as fast as possible, in which case I'd recommend the use
of __getitem__ because __getattr__ is invoked only after a lookup in the
object's local dictionary. In one case of use I've been working on, using
__getitem__ instead of __getattr__ makes the access to the fields go twice
as fast.

>
> Are there any other performance or compliance issues that need to be
> kept in mind when choosing between __getattr__ and __getitem__?   Would
> it be too extravagant to support both styles of access?

I don't think so, but, at least, it violates a Python mantra:

"It should preferably be one, and only one, way to do a thing"
(or something similar)

Cheers,

-- 
Francesc Alted




More information about the Pyrex mailing list