On Tue, Nov 2, 2010 at 11:56 PM, Stefan Behnel <[email protected]> wrote: > Robert Bradshaw, 03.11.2010 07:29: >> On Tue, Nov 2, 2010 at 10:13 PM, Stefan Behnel wrote: >>> What about something like >>> >>> cdef class PythonVector2d: >>> cdef Vector obj >>> cdef public x from obj.x >> >> I think of "cdef" as something accessible from c. This could be OK it >> did c-level attribute renaming as well. (Hmm... maybe I'd be OK with >> that, but it feels a bit odd to create this alias on the C level.) >> >>> ? I'm wondering if it isn't better to keep the existing attribute syntax >>> than to introduce a new property syntax. Although, a counter argument could >>> be that it isn't really an attribute... >> >> Yep. It's nice to look at the class and be able to visualize its struct. > > Agreed. > > >>> Next try: >>> >>> cdef class PythonVector2d: >>> cdef Vector obj: >>> property x >>> property readonly y as my_y >>> >>> Makes it clearer where the properties actually come from. >> >> This doesn't allow arbitrary expressions. > > Well, you can't allow "arbitrary expressions" since you will end up having > to assign to them in the setter. You can only allow arbitrary attribute > paths, which isn't prevented by the above. > > We could distinguish between normal and read-only properties, though, and > allow pretty much arbitrary expressions for the latter. Basically like a > lambda expression wrapped into a property descriptor.
Yep, that's what I was thinking. Attribute access paths would probably be the most common, but no need to restrict ourselves for the getter. >> On that note, should we >> require an explicit self.obj.x, or is self implicit? (The latter makes >> it impossible to refer to non-attributes, and is bad according to the >> zen of Python...), so I'd lean against this one. > > That's a very good idea. Makes it more readable and a lot clearer what > happens. This also removes the link to C++ as you can access any attribute > of the type in one way or another. > > Would you actually require it to refer to "self", or would you allow > arbitrary global names? I wouldn't even mind the latter, although maybe > restricted to static cdef declared names (at least of we generate a setter). Hmm.... I would at least generate a warning if not assigning to a self attribute. >>> Anyway, I won't object to your plain "property ... as ..." proposal above. >> >> Or "property ... from ..." if the name should be first (I don't have a >> strong preference, but the as is a more idiomatic keyword to use >> here). > > I think it makes sense to put the name first as the access path can be > arbitrarily long. It also simplifies the syntax as the keyword "property" > is then always followed by a name, then either ":" or "from" to distinguish > the two cases. +1 > We could also use > > property x as if self.obj.x > > but I think "from" reads well enough here. ;-) Yeah, I like "from." - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
