[MERGE] Support for lazy registation of commands

Alexander Belchenko bialix at ukr.net
Sun Aug 24 10:11:58 BST 2008


Lukáš Lalinský пишет:
> Dňa Ne, 2008-08-24 o 12:03 +0300, Alexander Belchenko napísal:
>> Lukáš Lalinský пишет:
>>> Dňa Ne, 2008-08-24 o 11:50 +0300, Alexander Belchenko napísal:
>>>> +class LazyCommandProxy(object):
>>>> +
>>>> +    def __init__(self, module, name, aliases):
>>>> +        self._module = module
>>>> +        self._name = name
>>>> +        self.aliases = aliases
>>>> +        self.__name__ = name
>>>> +
>>>> +    def __call__(self, *args, **kwargs):
>>>> +        cmd_module = __import__(self._module, globals(), locals(), [True])
>>>> +        cmd_class = getattr(cmd_module, self._name)
>>>> +        return cmd_class(*args, **kwargs)
>>>>
>>>>
>>>> Why for you pass [True] as last argument to __import__? IMO, you don't need
>>>> to pass there anything. According to Python documentation 4th argument
>>>> lists the names that should be imported from module. So importing name
>>>> 'True' is useless IIUC.
>>> Because self._module might be in form 'bzrlib.plugins.foo' and I want the 'foo'
>>> module, not 'bzrlib':
>>>
>>>   When the name variable is of the form package.module, normally, the
>>>   top-level package (the name up till the first dot) is returned, not the
>>>   module named by name. However, when a non-empty fromlist argument is
>>>   given, the module named by name is returned.
>> Why in this case you do not use [self._name]?
> 
> Because it's not necessary, the fromlist is not used for anything but
> checking that it's not empty.

You're right of course, but it's seems a bit hackish for me.
It's not explicit. :-P




More information about the bazaar mailing list