Message270658
Hello,
I've found an issue in python 2.7 and 3.4 and I don't if this is a bug or a feature that acts strange to me.
The import of a module or method from a module creates a reference in the package to that module only the first time, which could lead to unexpected behavior.
Description:
In following code there's one line marked with 'this line fixes the code to what I expected'
1. Without that line package.a.test() results in 'from module a'
2. With the line package.a.test() results in 'from module b'
Situation 1 is unexpected because I did not create the reference to 'module a', python did that with the statement 'from package.a import test' and this will happen from any place in the code that loads 'module a' for the first time. The documentation says that this reference will not be created.
Kind regards,
Marc
# FILES USED
#
# test.py
# package
# __init__.py
# a.py
# b.py
# ---------------------
# Content of a.py:
def test():
print('from module a')
# ---------------------
# Content of a.py:
def test():
print('from module b')
# ---------------------
# Content of __init__.py
#import a # <--- this line fixes the code to what I expected
import b as a
from a import test
# ---------------------
# Content of test.py
import package
print(dir(package))
package.a.test() |
|
Date |
User |
Action |
Args |
2016-07-17 18:14:01 | m.nijland | set | recipients:
+ m.nijland |
2016-07-17 18:14:01 | m.nijland | set | messageid: <1468779241.06.0.301525903069.issue27543@psf.upfronthosting.co.za> |
2016-07-17 18:14:01 | m.nijland | link | issue27543 messages |
2016-07-17 18:14:00 | m.nijland | create | |
|