[Python-checkins] r68919 - in python/branches/py3k: Doc/library/importlib.rstLib/importlib/NOTES Lib/importlib/_bootstrap.py

Brett Cannon brett at python.org
Sun Jan 25 22:14:11 CET 2009


On Sun, Jan 25, 2009 at 05:34, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Antoine Pitrou wrote:
>> <brett.cannon> writes:
>>> +    Only class methods are defined by this class to alleviate the need for
>>> +    instantiation.
>>
>> Why is it so? Is there some obvious performance improvement?
>> Instantiation and normal methods are generally a more intuitive coding style
>> than having only classmethods.
>> Also, it means you can have several instances with different parameters each,
>> and it eases testing (you can monkeypatch a test-specific instance without
>> disrupting the whole stuff).
>
> Using a class with only class methods is the easiest way to implement a
> singleton in Python.
>
> Since the BuiltinImporter and FrozenImporter are singletons by
> definition, it makes sense to implement them that way.

What Nick said: there is absolutely no state needed for these
importers to work so I didn't feel the need to require instantiation.
Just because the methods are all class methods doesn't mean you can't
instantiate the classes and use them, it just is superfluous.

As a perk it (will) also simplify the import code thanks to these two
importers being implicitly on sys.meta_path. By not having to
instantiate them there is that much less overhead, plus it helps do
away with implicit state for import which makes it more fragile.

-Brett


More information about the Python-checkins mailing list