Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Darkly)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Uncategorized
  3. Can someone explain this #Python import behavior

Can someone explain this #Python import behavior

Scheduled Pinned Locked Moved Uncategorized
python
23 Posts 10 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Baptiste MispelonB Baptiste Mispelon

    @treyhunner Tagging you on this since it might qualify as a #Pythonoddity

    Trey Hunner 🐍T This user is from outside of this forum
    Trey Hunner 🐍T This user is from outside of this forum
    Trey Hunner 🐍
    wrote last edited by
    #21

    @bmispelon This is absolutely a Python oddity. I guessed incorrectly. I understand why I guessed incorrectly now that I look back at the code... I'm not sure any Python oddity has stress testeded my mental model of Python's import system as much as this one.

    1 Reply Last reply
    0
    • Baptiste MispelonB Baptiste Mispelon

      Can someone explain this #Python import behavior?
      I'm in a directory with 3 files:

      a.py contains `A = 1; from b import *`
      b.py contains `from a import *; A += 1`
      c.py contains `from a import A; print(A)`

      Can you guess and explain what happens when you run `python c.py`?

      StylusS This user is from outside of this forum
      StylusS This user is from outside of this forum
      Stylus
      wrote last edited by
      #22

      @bmispelon

      $ echo 'A = 1; print("A1"); from b import A; print("A2")' > a.py
      $ echo 'print("B1"); from a import A; print("B2"); A += 1' > b.py
      $ python -c 'from a import A; print(A)'
      A1
      B1
      B2
      A2
      2

      I added several prints so that it's possible to tell what order code is executed, and changed import * to import A because I think it improves clarity without changing the behavior.

      • The main program runs
      • It encounters an import of a so it starts executing the content of a.py in a newly created a module
      • It sets A.a=1 via the assignment statement in a.py
      • It encounters an import of b so it starts executing the content of b.py in a newly created b module
      • It sets b.A=1 by from...import
      • It adds 1 to b.A so that b.A is now equal to 2
      • Execution reaches the end of b.py so it returns to a.py
      • a.py sets a.A to 2 by from...import
      • Execution reaches the end of a.py so it returns to the main program.
      • The main program sets __main__.A to 2 by from ...import
      • The value of A is printed (2)
      1 Reply Last reply
      0
      • Baptiste MispelonB Baptiste Mispelon

        Can someone explain this #Python import behavior?
        I'm in a directory with 3 files:

        a.py contains `A = 1; from b import *`
        b.py contains `from a import *; A += 1`
        c.py contains `from a import A; print(A)`

        Can you guess and explain what happens when you run `python c.py`?

        jlapoutreJ This user is from outside of this forum
        jlapoutreJ This user is from outside of this forum
        jlapoutre
        wrote last edited by
        #23

        @bmispelon My reasoning was the last option, 2, and then I saw what @pawamoy wrote, which makes perfect sense to me

        1 Reply Last reply
        0
        • R ActivityRelay shared this topic
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        Powered by NodeBB Contributors
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups