Adding a __filename__ predefined attribute to 2.5?

Rune Strand rune.strand at
Wed Oct 12 23:01:23 CEST 2005

> 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?
Maybe I'm wrong here, and accessing the filesystem and reading the
module into memory represents no cost. My mistake, in that case.

> wow.  that's one lousy optimization...

> here's a *shorter* piece of code, which is also readable and portable, and
> a lot easier to type on most keyboards:

>   import os
>  __filename__ = os.path.basename(__file__)

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.

More information about the Python-list mailing list