Package name with '.' in them: Python Bug ?
Terry Reedy
tjreedy at udel.edu
Fri Mar 19 12:32:12 EST 2004
"Yannick Patois" <patois at calvix.org> wrote in message
news:c3f1ac$n29$1 at sunnews.cern.ch...
> Hi,
>
> Under some naming conditions of module files, it seems that python lost
> class static variables values.
Since 'static variable' is not a Python concept, I do not know what you
mean.
> It seems only to append when importing a "badly" named module that
> itself import a module with a static variable in it (looks complex, but
> see the example below, pretty simple):
>
> First a working example:
> <<<< file: aTest.py
> #! /usr/bin/env python
> import imp
> import A
> name='B'
> fp, pathname, description = imp.find_module(name)
> B=imp.load_module(name, fp, pathname, description)
> a=A.A(1)
> b=B.B()
> >>>>
>
> <<<< file: A.py
> class A:
> a=None
> def __init__(self,a=None):
> if (a):
> A.a=a
> def __str__(self):
> return str(A.a)
> >>>>
>
> <<<< file: B.py
> import A
> class B:
> def __init__(self):
> a=A.A()
> print a
> >>>>
>
> Execution:
> $ ./aTest.py
> 1
>
> The value 1 obtained as expected (A.a is a static value and keept as
such).
Given that you rebound A.a to 1 from None, I especially do not know what
you mean.
> If now I just *rename* file B to B.1, without any change in the code
> (except name='B' become name='B.1' in aTest.py), content of B.1.py file
> being exacty the same as content of B.py, I get:
As you seem to be aware, Python expects modules to have a 'nice' name
consisting of
legal_Python_indentifier.py. If you evade this, you are a bit on your own
and may run into untested corner cases.
> $ ./aTest.py
> None
>
> Renaming B.py to B.1.py made A unable to keeps the value of it's static
> variable.
It seems to me that it *kept* its original value. I would put more print
statements into both A.A and B.B and maybe even aTest.py to see where
execution first differs.
> Bug tested with:
> Python 1.5.2
> Python 2.2.2
> Python 2.3.3
>
> Any idea ? Is it well a bug ? Some feature I didnt understood ? I read
> about submodule naminig using dots as separator, but I cant relate it
> towhat I saw here.
import b.b1 would normally mean import file (module) b1 in the b package
directory. But imp obviously did not find fine '1' in a 'B' directory.
Terry J. Reedy
More information about the Python-list
mailing list