1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How does Python decide whether the last part of an imported name is a module or a...

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    My first question is whether the code in main.py is valid?

    This is main.py:

    import foo.bar as fb
    fb()


    This is foo/__init__.py:

    from .bar import * # DELETE THIS
    bar = lambda: print('lambda bar')


    foo/bar.py is empty. Python version is 3.12.3.

    The second question is why deleting the first line from foo/__init__.py causes an error "TypeError: 'module' object is not callable"? Is this undefined behavior, implementation defined or documented somewhere? How does Python decide whether an imported name refers to a function or a submodule?

    Continue reading...

Compartilhe esta Página