python - Is it pythonic to write decorators that doesn't return a callable? -
i observed python decorator need not return callable. tried below example in django queryset. how bad practice?
class myqueryset(models.queryset): ... def each(self, save=false, func=none): if hasattr(save, '__call__'): func = save save = false obj in self: func(obj) if save: obj.save() mymodel.objects.each(lambda x: x.delete()) # delete mymodels database @person.objects.each(save=true): def make_older(person): ''' increase age of people ''' person.age += 1
ps:
are there situations technique can used more useful purpose?
from python glossary:
decorator
a function returning function, applied function transformation using @wrapper syntax. common examples decorators classmethod() , staticmethod().
edit: decorator not return callable not fall under official defnition of term 'decorator'. unpythonic.
Comments
Post a Comment