473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Function definition

I have a little .py file that has the format:
def fxn(a):
do stuff

other stuff
....
r = fxn(a)
.....
Now I've tried putting the function declaration after the call but the
program wouldn't work. Is there anyway to put function declarations at
the end of the program, rather than putting them at the beginning,
which is rather clunky?

Thanks.
Aaron

Jun 19 '06 #1
4 1504
no.
python is not C. python is interpreted, not compiled, so if you want a
function to exist when you call it, you need to define it before you
call it.
it isn't clunky, it's just how it's done.
if you want to define a 'main' function at the top of your
script/module, go for it. then you can use this:
if __name__ == '__main__': sys.exit(main(*sys.argv[1:]))

aarondesk wrote:
I have a little .py file that has the format:
def fxn(a):
do stuff

other stuff
...
r = fxn(a)
....
Now I've tried putting the function declaration after the call but the
program wouldn't work. Is there anyway to put function declarations at
the end of the program, rather than putting them at the beginning,
which is rather clunky?

Thanks.
Aaron


Jun 19 '06 #2
aarondesk wrote:
....

Now I've tried putting the function declaration after the call but the
program wouldn't work. Is there anyway to put function declarations at
the end of the program, rather than putting them at the beginning,
which is rather clunky?

Thanks.
Aaron


A function can call functions defined "below" it:

def a():
return b()

def b():
return 'foo'

print a() # prints 'foo'

because at runtime both functions exist in the current namespace. But
module-level statements can't call functions defined below them because
the function objects haven't been created yet. That's why you get a
NameError, the name 'b' won't been bound to anything until after the
def statement has been interpreted.

You can, of course, put your functions in another module.

Hope that helps,
~Simon

Jun 20 '06 #3
aarondesk wrote:
I have a little .py file that has the format:
def fxn(a):
do stuff

other stuff
...
r = fxn(a)
....
Now I've tried putting the function declaration after the call but the
program wouldn't work. Is there anyway to put function declarations at
the end of the program, rather than putting them at the beginning,
which is rather clunky?


How about putting your main program code inside another function called
something like 'main'? Then it can appear wherever you like. All you need
at the end of the file then is:

if __name__=='__main__':
main()

This also means you can reuse the functions with different main code simply
by importing them from another file.
Jun 20 '06 #4
faulkner wrote:
(pelase don't top-post - fixed)
aarondesk wrote:
(snip)
Now I've tried putting the function declaration after the call but the
program wouldn't work. Is there anyway to put function declarations at
the end of the program, rather than putting them at the beginning,
which is rather clunky?

no.
python is not C. python is interpreted, not compiled,


Please verify your assertions... FWIW, being "compiled" or "interpreted"
is not a feature of a language, but of an implementation of a language.
And FWIW also, CPython *is* compiled (to byte-code, like Java).

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
14
by: jg | last post by:
Does C++ standard require an inline function be generated all the time ? For example, #include <iostream> using namespace std; inline int foo() {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.