c - Diffrence between instance variables and attributes in python? -
so, python docs writing extension says this:
"we want expose our instance variables attributes. there number of ways that. simplest way define member definitions:
static pymemberdef noddy_members[] = { {"first", t_object_ex, offsetof(noddy, first), 0, "first name"}, {"last", t_object_ex, offsetof(noddy, last), 0, "last name"}, {"number", t_int, offsetof(noddy, number), 0, "noddy number"}, {null} /* sentinel */ };
and put definitions in tp_members slot:
noddy_members, /* tp_members */"
however, have put instance variables in noddy struct:
typedef struct { pyobject_head pyobject *first; pyobject *last; int number; } noddy;
so question why put them in both places. impression that, because want both type , instance have them preserve types values once instance updated. if thats case, how instance value updated if change class attribute? this:
>>> class foo(object): x = 4 ... >>> f = foo() >>> f.x 4 >>> foo.x = 5 >>> f.x 5
writing c extension complex process, because have write c code, , have provide enough information python can manipulate c data if python data. have mention members of struct twice: once c compiler know how make struct, , again python knows data is, , called.
c has no introspection ability, can't (for example), list of members of struct @ runtime. array of pymemberdef provides information.
Comments
Post a Comment