Brett Calcott, 04.11.2010 07:47:
> On 4 November 2010 16:19, Robert Bradshaw wrote:
>> On Wed, Nov 3, 2010 at 8:51 PM, Brett Calcott wrote:
>>> Is this possible?
>>> x = member_property(self.obj.x)
>>> y = getset_property(self.obj.getY, self.obj.setY)
>>> (the names of these need work I agree)
>>>
>>> But I take it that the general idea would be able to roll your own
>>> when there was some repetitive thing you needed to do, so up to you
>>> what you passed.
>>>
>>> For example, the obj pointer in some of the classes I am currently
>>> wrapping can sometimes be set to NULL. So in many cases the property
>>> access needs to check that first, and raise an exception. If I could
>>> write my own getset_with_check_property(...), that would be very cool.
>>
>> If you need more than something simple, is the
>>
>> property x:
>>     def __get__(self):
>>         [some logic here]
>>         return result
>>
>> too verbose for you? We're not trying to be perl with magic one-liners.
>
> I think we've lost the context of my original question. I'm wrapping a
> C++ library. If I just had to write one of these functions, then
> indeed trying to do it as a one-liner would be silly. But I'm wrapping
> lots of them, and this means I'm just cutting and pasting code, and
> then replacing names. When I find myself doing that, I think: there
> has to be a better way. The problem with perl one-liners is not that
> they are short, but that they are often obscure. But a concise and
> obvious one line mapping from a C++ member (or member functions) to a
> python property would be clearer, more concise, and easier to modify.
> It isn't so much the verbosity that is the problem (though brevity
> along with clarify is surely a good thing), it is the robotic
> repetition of code that I'm opposed to.

You may have missed my other e-mail, but to put it a bit clearer, there are 
three levels where Cython can provide support:

1) map an expression or class field to a property

This is what we were previously discussing in this thread, well in the 
context of your original question.

2) map a pair of setters and getters to a property

This can be done using Python's builtin property() type and further 
optimised (or enabled for C/C++ functions/methods) by Cython.

3) map more complex things to properties

Use the existing property syntax in Cython. It's a bit verbose for some 
special cases, but usually, special cases are not special enough to break 
the rules, unless practicality really beats purity. Point 1) above may or 
may not be seen as such a case.

IMHO, only point 1) is worth discussing as it appears to require new 
syntax. 2) simply needs an implementation and 3) is usable today.

Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to