function dysfunction #10394
Replies: 1 comment
-
The actual importing (i.e. looking up the module, parsing the code etc.) happens only upon the first import statement. The second one won't actually do anything: Python internals keep track of what has been imported already.
In general it's better to not do this except for cases where the imported name is very unambiguous. Reasons: more chance of clashing names, less clear to read (because in general people don't do this), more tedious work because every function you need needs to be imported explicitly. So something like
To illustrate previous paragraph by example: suppose afterwards you also add functions2.py and both functions.py and functions2.py have a function dostuff(). Now if you use
Using global variables like that is fine in small and simple programs but turns into a mess when they become larger. It makes all your code stick together like one big ball of mud, without being able too reuse any funcionality. Keep the global variables in main.py, write reusable functions which take arguments in other modules, then pass the variables to those functions explicitly.
You're not, it's the way to go, for all the reasons you mention in the first paragraph, you just need to learn some more and get some experience. Might help to start with a small test progra or a subset of what you have now so you can focus on splitting it in a module insted of constantly having to weed out errors on every single change you make, just because there's a lot of code. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've got a bunch of functions that I use repeatedly so I thought I might put them in a module (the imaginatively named functions.py) & import them instead of copying them into each new program. This move, I naively figured, would leave just the application specific code in main.py, so a lot less scrolling past largely invariant functions when coding a new program. Also having all the functions in one place would mean that if I made a minor improvement to one of them it would automatically be in place next time I imported from functions.py, unlike my current situation where I have multiple versions of _upld(), each slightly different as I've rolled the nugget in more & more glitter over the years.
I decided to make a move over the Xmas break.
First problem, in their new home in functions.py the functions had no access to the imports in main.py, so I had to copy over all the imports. This means sys, os, json, time, etc are now imported twice, once by functions.py (for the fucntions) and also in main.py for the applications specific code. This hasn't caused any ram memory error messages so far but I live in fear.
Second problem. Somewhere in my coding journey I'd swapped from having something like import time at the top of my program (& terms like time.time(), time.sleep() and time.gmtime() in the code) to explicitly importing what I wanted (from time import time, sleep, gmtime). I think I might have done this to save typing but it has made the necessity of copying the necessary imports into functions even more onerous.
Third problem. Running from main.py the functions knew all the variables, lists & dictionaries created by the applications specific code (ie the code not running as a function). Running from functions.py they don't.
Cut a long story short I've abandoned the idea for the time being. I feel I must be doing it wrong, that it shouldn't be this hard, that I should be able to get away with from functions import * instead of having to laboriously list all the functions in functions.py. If anyone has a few helpful suggestions (as opposed to criticisms) I'm all ears.
Beta Was this translation helpful? Give feedback.
All reactions