Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import vs inserting code
#3
(Jun-14-2024, 01:18 AM)Skaperen Wrote: is there any circumstance in which inserting code, such as while editing, has a different result than doing an import of the same code
@rodiongork 's answer is excellent. When you import a module, this module's code is executed in the new module's namespace. When you insert code, it is executed in the namespace of the module where the code is inserted.

This can have unpredictable consequences. Variables can be shadowed (this also includes functions and classes), code depending on the module name could behave differently, for example if __name__ == '__main__' code, etc.
rodiongork likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Messages In This Thread
import vs inserting code - by Skaperen - Jun-14-2024, 01:18 AM
RE: import vs inserting code - by rodiongork - Jun-19-2024, 07:55 PM
RE: import vs inserting code - by Gribouillis - Jun-19-2024, 09:42 PM
RE: import vs inserting code - by Skaperen - Jun-20-2024, 10:38 PM
RE: import vs inserting code - by rodiongork - Jun-21-2024, 05:47 AM
RE: import vs inserting code - by Skaperen - Jun-22-2024, 01:21 AM
RE: import vs inserting code - by Gribouillis - Jun-22-2024, 08:14 AM
RE: import vs inserting code - by Skaperen - Jun-23-2024, 01:54 AM
RE: import vs inserting code - by Gribouillis - Jun-23-2024, 06:52 AM
RE: import vs inserting code - by AdamHensley - Jun-23-2024, 12:02 PM
RE: import vs inserting code - by Skaperen - Jun-26-2024, 09:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  visual studio code python how to get source code of import module definitian umen 2 4,196 Jun-13-2020, 06:20 PM
Last Post: umen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020