[Grok-dev] @grok.traverse versus traverse

Martijn Faassen faassen at infrae.com
Thu Oct 19 07:51:45 EDT 2006


Christian Theune wrote:
[snip]
> Sounds reasonable. However, what about this scenario: You have a model
> class that defines multiple "getFoo" methods, like:
> 
> class House(grok.Model):
> 
> 	def getEntrance(self, name):
> 		return ...
> 
> 	def getWindow(self, name):
> 		return
> 
> 	def getLevel(self, level):
> 		return ...
> 
> With the decorator, I could say @grok.traverse to select one of those
> for traversal without rewriting anything. With before I'd have to do an
> indirection with code, I don't know how often this pattern would
> actually set in, but I'd like to be able to use a decorator in this
> case, I think.
>

Adding a decorator saves you 1 line of code and is a trifle less DRY, 
but still, this isn't very disastrous:

class House(grok.Model):
    def traverse(self, name):
        return self.getWindow(name)

Often enough there needs to be some mangling of the name anyway, for 
instance int(name) and whatnot. The case when you can get away with just 
using a decorator is therefore rather limited anyway, so I'd say YAGNI 
to that.

Regards,

Martijn


More information about the Grok-dev mailing list