Does python not have a way to predeclare functions, so I can keep 'main()' at the top and the utils at the bottom of a file? AFAIK, I have to go: ######################################## # Utility functions start here def util_func(): return 'a value' # Main starts here do_util() ######################################## but for readability I'd much rather go: ######################################## <some text to predeclare the function goes here> # Main starts here do_util() # Utility functions start here def util_func(): return 'a value' ######################################## Anyone know? I can live without it, but I like to have 'main' at the top so I can see what's supposed to be going on without scrolling to the bottom of a file first... Cheers -------------------------------------- Phil Robinson. philrobinson@ponytrot.net --------------------------------------
philrobinson wrote at 2003-1-12 14:28 -0000:
Does python not have a way to predeclare functions, so I can keep 'main()' at the top and the utils at the bottom of a file? What prevents you from defining "main" at the top?
In fact, you can. Only the call to main must be after all referenced global (external) names have been defined. Dieter
Sorry, I was not clear enough: It's a zope python script, so I'm not actually declaring a main() function I want to do this (in a Zope 'Script (Python)') ++++++++++++++++++++++++++++++++++++++++ # Main starts here do something... retval = do_util() do something else return something... # Utility functions start here def util_func(): return 'a value' ++++++++++++++++++++++++++++++++++++++++ But I can't because zope doesn't know how to call do_util. In c, I could just predeclare the function then use it without worrying about the actual definition which comes along later. Is this possible? Thanks, -------------------------------------- Phil Robinson. philrobinson@ponytrot.net --------------------------------------
philrobinson wrote at 2003-1-12 14:28 -0000:
Does python not have a way to predeclare functions, so I can keep 'main()' at the top and the utils at the bottom of a file? What prevents you from defining "main" at the top?
In fact, you can.
Only the call to main must be after all referenced global (external) names have been defined.
Dieter
philrobinson wrote:
Sorry, I was not clear enough: It's a zope python script, so I'm not actually declaring a main() function
I want to do this (in a Zope 'Script (Python)') ++++++++++++++++++++++++++++++++++++++++ # Main starts here do something... retval = do_util() do something else return something...
# Utility functions start here def util_func(): return 'a value' ++++++++++++++++++++++++++++++++++++++++
Try this:
++++++++++++++++++++++++++++++++++++++++ # Utility functions start here def util_func(): return 'a value'
# Main starts here do something... retval = do_util() do something else return something...
++++++++++++++++++++++++++++++++++++++++
...alternatively, just put util_func in it's own Script (Python) (*grumble* these _so_ should have been caleld Python Functions or Python Methods) and call it like: context.util_func() ...cheers, Chris
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of philrobinson Sent: Monday, January 13, 2003 3:17 AM To: Dieter Maurer Cc: zope@zope.org Subject: Re: [Zope] function pre-declaration?
Sorry, I was not clear enough: It's a zope python script, so I'm not actually declaring a main() function
I want to do this (in a Zope 'Script (Python)') ++++++++++++++++++++++++++++++++++++++++ # Main starts here do something... retval = do_util() do something else return something...
# Utility functions start here def util_func(): return 'a value' ++++++++++++++++++++++++++++++++++++++++ But I can't because zope doesn't know how to call do_util. In c, I could just predeclare the function then use it without worrying about the actual definition which comes along later. Is this possible? Thanks,
Nope. It's a bad habit for a python programmer anyway. Put everything in functions, you'll be happier you did in the long run. Something like: def mymain(): dosomething... retval = util_func() def util_func(): return 'avalue' mymain()
participants (4)
-
Charlie Reiman -
Chris Withers -
Dieter Maurer -
philrobinson