Oct-10-2018, 05:18 AM
Pages: 1 2
Oct-10-2018, 08:57 AM
Can use
my_module.py:
A module is always fully imported,so doing
So
But whether you use anything else from the module or not,it's always fully imported.
Then can access all other names from it.
from my_module import d
my_module.py:
a = 100 b = 200 c = 300 d = 400bar.py:
from my_module import d print(d)
Output:400
So now have access to only d
in bar.py
.A module is always fully imported,so doing
import my_module
and from my_module import d
make no difference in bottom.So
sys.modules['my_module']
has reference to whole file.>>> import sys >>> sys.modules['my_module'] <module 'my_module' from 'E:\\div_code\\new\\my_module.py'>
from my_module import d
point straight to d
.But whether you use anything else from the module or not,it's always fully imported.
import my_module
binds the name my_module
to sys.modules['my_module']
.Then can access all other names from it.
>>> import my_module >>> my_module.a 100 >>> my_module.b 200 >>> my_module.d 400
Oct-10-2018, 04:08 PM
If you want to do this, then you probably have logical breaks in the module, and could redesign it as sub-modules, so you only import the small piece you want.
Oct-11-2018, 12:52 AM
is there a way to do it by line numbers?
Oct-11-2018, 02:58 AM
line numbers would be a bad way because they're a moving target.
Could be a disaster. I hope there's no built in method for that.
If for some reason you want to do something stupid, I guess you could write the code easily enough.
Could be a disaster. I hope there's no built in method for that.
If for some reason you want to do something stupid, I guess you could write the code easily enough.
Oct-11-2018, 03:25 AM
(Oct-11-2018, 12:52 AM)Skaperen Wrote: [ -> ]is there a way to do it by line numbers?
This sort of thinking is your problem. You are constantly saying "This is how I want to write my code. Can I do that in Python?"
This is the wrong way to think about programming in any language.
The way to think is "This is what I want to do. How can I do it in Python?" Note that there is no code involved in the first sentence.
Oct-13-2018, 05:21 AM
yes, line numbers could be a moving target. so now i want to import everything between the lines "#foobarstart" and "#foobarend".
this module is a bunch of variable settings. it has 2 groups of settings. originally i wanted to set a global variable and check it with an if/else that sets one group or the other. but the module could not see what the importer set.
this module is a bunch of variable settings. it has 2 groups of settings. originally i wanted to set a global variable and check it with an if/else that sets one group or the other. but the module could not see what the importer set.
Oct-13-2018, 11:58 AM
You're a lost cause!
Oct-13-2018, 08:22 PM
i already know that.
Oct-14-2018, 04:21 AM
(Oct-13-2018, 05:21 AM)Skaperen Wrote: [ -> ]so now i want to import everything between the lines "#foobarstart" and "#foobarend".
How about a different file for the things you sometimes want to import?
# foobar.py shared config here
# conf.py # foobarstart from foobar import * # foobarend #more config
# unrelated.py import foobar as shared_configOr, you can look at the
goto
april fool's module: http://entrian.com/goto/It works by installing a handler in python's stack trace, looking for certain errors, and "fixing" the script when those errors are thrown.
Pages: 1 2