Local variables initialization

Michal Kwiatkowski ruby at no.spam
Sun Feb 26 21:56:24 EST 2006


Michal Kwiatkowski wrote:
> def init_arguments(fun):
>     def new_f(self):
>         var_one = self.attr_one
>         var_two = self.attr_two.another_attr
>         empty_list = []
> 
>         fun(self, var_one, var_two, empty_list)
> 
>     return new_f
> 
> @init_arguments
> def method(self, var_one, var_two, empty_list):
>     # significant code goes here
>     # ...
> 
> But the bad thing about this approach is that actual method doesn't
> really look like its definition, because of different number of arguments.

I've come up with a solution that don't have this disadvantage. Here we go:

def wrap_method(self, do):
    var_one = self.attr_one
    var_two = self.attr_two.another_attr
    empty_list = []

    return do(self, var_one, var_two, empty_list)

def method(self):
    def do(self, var_one, var_two, empty_list):
        # significant code goes here
        # ...
    return self.wrap_method(do)

All is implicit and have an advantage of being clear in < 2.4, where
there are no nice @ decorator syntax. Again, simple things proved to be
better. :)

mk
-- 
 . o .       >>  http://joker.linuxstuff.pl  <<
 . . o   It's easier to get forgiveness for being wrong
 o o o   than forgiveness for being right.



More information about the Python-list mailing list