473,385 Members | 1,645 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.

python function defs/declarations

hi..

the docs state that the following is valid...

def foo():
i = 2
print "i = "i

print "hello"
foo()
is there a way for me to do this..

print "hello"
foo()

def foo():
i = 2
print "i = "i

ie, to use 'foo' prior to the declaration of 'foo'

thanks

-bruce

Jul 3 '06 #1
3 1423
bruce <be*******@earthlink.netwrote:
hi..

the docs state that the following is valid...

def foo():
i = 2
print "i = "i

print "hello"
foo()
is there a way for me to do this..

print "hello"
foo()

def foo():
i = 2
print "i = "i

ie, to use 'foo' prior to the declaration of 'foo'
There are no declarations in Python. "def" is an executable statement:
when executes it binds a new function object to the given name.

So, your request is like asking to do, say:

print "hello"
print wap

wap = "world"

At the time you use name wap, nothing is bound to it; the fact that
something would later be bound to it (if the binding statement, here an
assignment but that's exactly as much of an executable statement as a
def!) is pretty clearly irrelevant. Having a clear idea about these
issues is why it's important to remember the distinction between
executable statements (including def, class, assignments, ...) and
declarations (which Python does not have).

You can probably wrap your code in a function, and call it at the end:

def main():
print "hello"
foo()

def foo(): ...whatever...

main()
By the time the body of main executes, "def foo" has already executed,
so global name foo is happily bound and everything works fine.

Wrapping most substantial code inside functions is VERY advisable anyway
-- just put just about all the code you'd like to have at module top
level (except for def, class and assignments to "module constants") into
a function (conventionally named main) and call that function at the
very end of the module (ideally within an "if __name__=='__main__":"
guard, but that's a different issue!).
Alex
Jul 3 '06 #2
bruce wrote:
the docs state that the following is valid...
print "i = "i
Is this a typo or is that really in the docs?
Jul 5 '06 #3
bruce:
is there a way for me to do this..

print "hello"
foo()

def foo():
i = 2
print "i = "i

ie, to use 'foo' prior to the declaration of 'foo'
Generally no you can't, you have to define a name before using it.
Why do you want to do that?

Bye,
bearophile

Jul 5 '06 #4

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

Similar topics

10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
1
by: Jeremy C. Reed | last post by:
Configuring gramps (genealogy software) says: checking Python bindings for gtk... ok checking Python bindings for GNOME... ok checking Python bindings for gconf... Traceback (most recent call...
58
by: Svein Ove Aas | last post by:
Is anyone working on a python-to-native compiler? I'd be interested in taking a look. Come to think of it, is anyone working on a sexpr-enabled version of Python, or anything similar? I really...
3
by: James Stroud | last post by:
Hello All, Because of my poorly designing a database, I have recently found it necessary to explore the wonders of the Python pseudo-switch: do_case = { "A" : lambda x: x, "B" : lambda x: x,...
0
by: Putz Ronald | last post by:
Hy! I am trying to convert a win Oracle application to solaris! We have use external c functions which I have ibncluded in a shared object. See the makefile below: OBJECTS = definitions.o...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
86
by: Matthias Kaeppler | last post by:
Hi, sorry for my ignorance, but after reading the Python tutorial on python.org, I'm sort of, well surprised about the lack of OOP capabilities in python. Honestly, I don't even see the point at...
3
by: Paul Sijben | last post by:
I am running a multi-threaded python application in a dual core intel running Ubuntu. I am using python 2.5.1 that I compiled myself. At random points I am getting segmentation faults (sometimes...
3
by: Daniel Folkes | last post by:
I am new to using Vim's scripts. I was wondering if anyone uses Vim-Python and how to use it? This includes things like key bindings and such. Thanks in advance, Daniel Folkes...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.