<br><br><div><span class="gmail_quote">On 1/26/07, <b class="gmail_sendername">Nick Maclaren</b> <<a href="mailto:nmm1@cus.cam.ac.uk">nmm1@cus.cam.ac.uk</a>> wrote:</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Does Python use classic division (nb_divide) and inversion (nb_invert)<br>or are they entirely historical? Note that I can very easily provide<br>the latter.</blockquote><div><br>nb_divide is used for the division operation (/) when not using 'from __future__ import division', and PyNumber_Divide(). It doesn't fall back to foor_divide or true_divide.
<br></div>nb_invert is used for bitwise inversion (~) and PyNumber_Invert(). It's not historical, it's actual.<br><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Is there any documentation on the coercion function (nb_coerce)? It<br>seems to have unusual properties.</blockquote><div><br>I don't recall ever seeing useful documentation on coerce() and nb_coerce. I suggest not to use it; it's gone in Python
3.0 anyway.<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">How critical is the 'numeric' property of the nb_hash function? I
<br>can certainly honour it, but is it worth it?</blockquote><div><br>Which numeric property? the fact that it returns a C long? Or that, for natural numbers, it *seems* to return self? The former is quite important, the latter not very. The important bit is that an object's hash() be equal to the hash() of objects that it is considered equal to. That is to say, if you have 'd = {5: "five"}' and you want 'f = yourtype(5); d[f]' to result in "five", hash(f) must be equal to hash(5). The builtin floats do that. There's no strict requirement that equal objects must have equal hashes, but people using sets and dicts really appreciate it. ('f == 5 and f not in set([5])' to be True would be confusing.)
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I assume that Python will call nb_richcompare if defined and<br>nb_compare if not. Is that right?
</blockquote><div><br>Yes.<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Are the inplace methods used and, if so, what is their specification?
</blockquote><div><br>(Hah, my specialty.) Inplace methods are used for the augmented-assignment statements: '+=' and the like. They are free to modify 'self' or return a new object. Python falls back to the normal, non-inplace operations if the inplace methods are not defined. I assume your floating-point type is immutable, so you won't have to implement them. (If the type is to be mutable, don't forget to make them unhashable, by not defining nb_hash.)
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I assume that I can ignore all of the allocation, deallocation and<br>attribute handling functions, as the default for a VAR object is
<br>fine. That seems to work.<br><br>Except for one thing! My base type is static, but I create some<br>space for every derivation (and it can ONLY be used in derived form).<br>The space creation is donein C but the derivation in Python. I
<br>assume that I need a class (not instance) destructor, but what<br>should it do to free the space? Call C to Py_DECREF it?</blockquote><div><br>Where do you allocate this space, and how do you allocate it? If it's space you malloc() and store somewhere in the type struct, yecchh. You should not just allocate stuff at the end of the type struct, as the type struct's layout is not under your control (we actually extend the type struct as needed, which is why newer features end up in less logical places at the end of the struct ;) I would suggest using attributes of the type instead, with the normal Python refcounting. That means the 'extra space' has to be an actual Python object, though.
<br> </div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I assume that a class structure will never go away until after all<br>instances have gone away (unless I use Py_DECREF), so a C pointer
<br>from an instance to something owned by the class is OK.</blockquote><div><br>Correct. <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Is there any documentation on how to support marshalling/pickling<br>and the converse from C types?</blockquote><div><br>I don't you can make your own type marshallable. For pickle it's more or less the same as for Python types. The pickle docs (and maybe
<a href="http://www.python.org/dev/peps/pep-0307/">http://www.python.org/dev/peps/pep-0307/</a>) probably cover what you want to know. You can also look at one of the complexer builtin types that support pickling, like the datetime types.
<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I would quite like to provide some attributes. They are 'simple'<br>but need code executing to return them. I assume that means that
<br>they aren't simple enough, and have to be provided as methods<br>(like conjugate). That's what I have done, anyway.</blockquote><div><br>You can use PyGetSetDef to get 'easy' attributes with getters and setters.
<a href="http://docs.python.org/api/type-structs.html#l2h-1020">http://docs.python.org/api/type-structs.html#l2h-1020</a><br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Is there any obvious place for a reduction method to be hooked in?<br>That is a method that takes a sequence, all members of which must<br>be convertible to a single class, and returns a member of that<br>class. Note that it specifically does NOT make sense on a single
<br>value of that class.</blockquote><div><br>There's nothing I can think of that is a natural match for that in standard Python methods. I would suggest just making it a classmethod. (dict.fromkeys is a good example of a classmethod in C.)
<br><br>As a final note: Python's source itself is a good source of answers and examples. Almost all of the features of the Python API are used in a builtin type or stdlib module, and for C source, Python's source is remarkably readable ;-)
<br></div></div><br>-- <br>Thomas Wouters <<a href="mailto:thomas@python.org">thomas@python.org</a>><br><br>Hi! I'm a .signature virus! copy me into your .signature file to help me spread!