Adding a __filename__ predefined attribute to 2.5?

Fredrik Lundh fredrik at pythonware.com
Thu Oct 13 12:06:05 CEST 2005


Rune Strand wrote:

>> those modules are already imported when Python gets to your code, so
>> the only "overhead" you're saving is a little typing.
>
> I don't understand this. Could you please elaborate?  - if sys or os
> are not imported for any other causes how are they already imported?

because they are imported for Python's own purposes, together with lots
of other stuff:

    > python
    Python 2.4.2
    >>> import sys
    >>> sys.modules.keys()
    ['copy_reg', 'locale', '__main__', 'site', '__builtin__', 'encodings', 'os.path'
    , 'encodings.cp437', 'encodings.codecs', 'ntpath', 'UserDict', 'encodings.except
    ions', 'nt', 'stat', 'zipimport', 'warnings', 'encodings.types', '_codecs', 'enc
    odings.cp1252', 'sys', 'codecs', 'types', '_locale', 'signal', 'linecache', 'enc
    odings.aliases', 'exceptions', 'os']

(sys is a built-in module, btw, so the cost of importing that is always close to zero)

> It may be lousy, but it requires no imports. And, as I said in the
> answer to Steve, I _know_ there are many ways to achieve this,
> including yours. But in your rush to pin-point lousy code, you didn't
> read that, I suppose.

you know, being clueless is one thing, but being both clueless and arrogant is
not a good way to get anywhere.  I suggest spending more time learning things
(start with the language reference), and less time picking fights that leads no-
where.

</F> 






More information about the Python-list mailing list