Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
global varname
#6
This is so weird. Just a day or two ago there was another thread wondering if there was a way to clump together imports so they are all done at once. I am so averse to changing anything in builtins that it didn't occure to me that adding import objects to builtins would do exactly what he wanted. That still doesn't stop it from being a REALLY BAD IDEA.

file imports.py
import math, builtins  # As long as I am doing bad things
builtins.math = math
file test.py
import imports  # Only need to do this once.  Now math is in builtins
print(math.cos(math.pi))
Output:
-1
This works. After math gets added to builtins, other files don't even have to import "imports". If I set this up as a package, I could pollute the builtins namespace inside an __init__.py file and make it even harder to track down when math was promoted to a builtin. This not only creates confusion when anyone reads your code, it also hamstrings nice programming tools like autocomplete, snippets and kite. pylance hates the code: 'imprts' imported but not used, 'math' is not defined.
Skaperen likes this post
Reply


Messages In This Thread
global varname - by Skaperen - Mar-01-2023, 11:39 PM
RE: global varname - by snippsat - Mar-02-2023, 02:25 AM
RE: global varname - by Skaperen - Mar-02-2023, 05:10 AM
RE: global varname - by Gribouillis - Mar-02-2023, 06:31 AM
RE: global varname - by snippsat - Mar-02-2023, 09:32 AM
RE: global varname - by deanhystad - Mar-02-2023, 06:35 PM
RE: global varname - by Skaperen - Mar-03-2023, 12:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variable does not seem to be global. Columbo 6 3,912 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

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