[Pyrex] getattr

Stefan Behnel stefan_ml at behnel.de
Tue Oct 9 11:31:44 CEST 2007


Greg Ewing wrote:
> Martin Field wrote:
>> I guess
>> the Python version can take 3 arguments (the third being optional)
>> whereas the API version only takes 2. Is there some (easy!) way
>> of mimicking this behaviour in Pyrex
> 
> You could write yourself a function
> 
>    cdef object getattr3(object obj, char *attr, object default):
>      try:
>        return getattr(obj, attr)
>      except AttributeError:
>        return default
> 
> I may include something like this as a Pyrex builtin
> if the current behaviour turns out to be too much of a
> nuisance.

We also noticed that in Cython and are not currently replacing getattr with
the C function. It would actually be cool if Pyrex/Cython could distinguish
between getattr/getattr3 based on the number of arguments getattr() is called
with (i.e. no new function name as an additional builtin).

Stefan



More information about the Pyrex mailing list