User avatar
quarkjuggler
Posts: 156
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Ogranizing Python program

Wed Feb 15, 2017 4:09 am

Is there an accepted way to properly sequence the functions in a python module based on which are referenced by what others? Perhaps there's software to do it? Efficiency is not nearly as important to me as readability. I've got 50 functions so far, and I'm sure to have a bunch more before I'm done, and the menagerie would be easier to manage if there were some logical sequence for these functions. --Any suggestions?
Statements resembling facts are IMO only. "he" means "he or she", etc. "Mother Nature" is an abstraction, not a real spirit.
John

User avatar
DougieLawson
Posts: 35381
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Ogranizing Python program

Wed Feb 15, 2017 8:48 am

It doesn't matter as long as a function is defined earlier in the source than the point where you try to run it.

So lowest level functions come first.

If things are getting ugly and unweildy then you should break your massive pile of spaghetti into a series of imported files.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: Ogranizing Python program

Wed Feb 15, 2017 9:03 am

Just a small correction: python (unlike c) allows forward reference, so you can put functions (def) anywhere you want (so also after you refer it first time).

User avatar
DougieLawson
Posts: 35381
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Ogranizing Python program

Wed Feb 15, 2017 9:57 am

shuckle wrote:Just a small correction: python (unlike c) allows forward reference, so you can put functions (def) anywhere you want (so also after you refer it first time).
OK. But it still makes sense to do bottom up programming by defining low level routines first. That way you know what you expect.

importing things is 10^6 times better than a bucket of spaghetti. The imported routines then become reusable for other projects.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
quarkjuggler
Posts: 156
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Ogranizing Python program

Thu Feb 16, 2017 2:23 am

On the iSeries, back before they went with 'free' code, everything was in columns -- very easy to parse, so I had a program that would treat a program like a bill of material, to put like what you're talking about, but upside down, since the mainline had to be first. I'd like to do something like that in Python, but things are a bit more complicated now. What do you do with functions dedicated to one thread or another? Do you separate things by the thread they run under? "main" goes at the bottom, then should I put all 2nd-level above that before going to any 3rd-level, or put 3rd-level immediately above functions that they serve? I haven't seen anywhere on the web that discusses this.

I'm with you on splitting things up into modules, and I'm already using two sources. But I use global variables to communicate between threads, so that glues a lot of stuff together. Is this the way it's done?
Statements resembling facts are IMO only. "he" means "he or she", etc. "Mother Nature" is an abstraction, not a real spirit.
John

User avatar
DougieLawson
Posts: 35381
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Ogranizing Python program

Thu Feb 16, 2017 9:12 am

When you import the imported code is in the same namespace as the mainline code that imported it. You should avoid global variables whenever you can. Pass everything by reference on the function header and return where possible.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
quarkjuggler
Posts: 156
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Ogranizing Python program

Thu Feb 16, 2017 9:24 am

Thanks, DougieLawson! You've inspired me to change a bunch of stuff, so I'll disappear for awhile again.

--John
Statements resembling facts are IMO only. "he" means "he or she", etc. "Mother Nature" is an abstraction, not a real spirit.
John

Return to “Beginners”