On 4 November 2010 16:19, Robert Bradshaw <[email protected]> wrote: > On Wed, Nov 3, 2010 at 8:51 PM, Brett Calcott <[email protected]> wrote: >> On 4 November 2010 14:34, Robert Bradshaw <[email protected]> >> wrote: >>> On Wed, Nov 3, 2010 at 8:24 PM, Brett Calcott <[email protected]> >>> wrote: >>>> On 3 November 2010 19:24, Dag Sverre Seljebotn >>>> <[email protected]> wrote: >>>>> cdef class A: >>>>> cdef Vector obj >>>>> x = delegate_property(obj, 'x') >>>>> >>>>> or similar, without throwing in new syntax. But it may be too late and I >>>>> don't care too much. Remember that "x = property(...)" is already in >>>>> Python. >>>>> >>>> >>>> This looks like an interesting option, especially if it was possible >>>> to write your own reusable "xxxx_property" functions that did "the >>>> right thing" for the particular object/library being wrapped. This >>>> would remove a lot boilerplate code that is normally cut and pasted >>>> for setter/getter functions. So, in a case where the C++ members are >>>> private, but access is provided via camelcase set/get functions, >>>> instead of >>>> >>>> property value: >>>> def __get__(self): >>>> return self.obj.getValue() >>>> def __set__(self, int x): >>>> self.obj.setValue(x) >>>> >>>> We could write: >>>> x = camelcase_delegate_property(self.obj, 'value') >>> >>> The one thing that I don't like about this is putting the attribute in >>> a String. >>> >> >> 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. Brett _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
