C-API, tp_dictoffset vs tp_members

"Martin v. Löwis" martin at v.loewis.de
Sun Jul 26 14:03:39 EDT 2009


>>> I have a predefined set of members, some of which are optional.
>> Having optional fields is also a good reason.
> 
> What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then?

Right - this works for optional objects. However, it can't possibly
work for any of the other fields.

>  I would
>  have though that the former describes an optional field, because the
>  behaviour of accessing it when it is NULL is the same as accessing a
>  nonexistent field. However, I see that it still appears in the dir()
>  output even if it is NULL, so it seems I'm misunderstanding this.

I suppose that's because there will still be a descriptor for the field
in the class.

> The 'example_attribute' can be accessed, but it is not visible in the
> dir() output. The code for this is below.

I see. So you do need a tp_members list:

static PyMemberDef example_members[] = {
     {"__dict__", T_OBJECT, offsetof(Example, dict), READONLY},
     {0}
};

Perhaps dir() should know about tp_dictoffset, but alas, it doesn't.

Regards,
Martin



More information about the Python-list mailing list