On 16Feb2024 20:32, MRAB <pyt...@mrabarnett.plus.com> wrote:
On 2024-02-16 20:07, Gabor Urban via Python-list wrote:
I need something about modules to be clarified.

Suppose I have written a module eg: ModuleA which imports an other
module, let us say the datetime.

If I import ModuleA in a script, will be datetime imported automatically?

Yes. When a module is imported it can import other modules.

But note that `datetime` does not magicly get put in the script's namespace.

Module A:

    import datetime

Script:

    import A

In the code in module A the name datetime is known and can be used.

In the code in the script the name A is known and can be used. Importing A does not magicly set the name datetime in the script's namespace - imagine the the pollution!

You _can_ access it as A.datetime because it is in the A module's namespace. But really if you just wanted datetime for direct use in the script you would import it there too:

    import datetime
    import A

Note that the datetime module is only actually loaded once. The import binds the name into your local namespace like any other variable.

Cheers,
Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to