[Pyrex] __getitem__ vs __getattr__?
Joseph Koshy
jkoshy at FreeBSD.ORG
Sat May 24 02:48:12 CEST 2003
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__)?
Lets say we have a data type `T' that is a C structure with fields
'field_1', 'field_2' etc. We can make these fields accessible to Python
by supporting either attribute access and/or dictionary-style
access:
t = TWrapped()
A) x = t.field_1 # via __getattr__
B) x = t['field_1'] # via __getitem__
A plus point of allowing (B) would be that (print '%(field_1)s' % t) would
work. However, option (A) appears to be more convenient for the programmer.
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?
--
Joseph Koshy <jkoshy at freebsd.org>
Developer, The FreeBSD Project http://people.freebsd.org/~jkoshy/
More information about the Pyrex
mailing list