473,378 Members | 1,478 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,378 software developers and data experts.

about functions question

I try it:

def b():
...
a()
...

def a():
...
b()
...

b()
it's not work.

Is it possible pre-define function like in c++ or place functions code
after main block?

int a();
int b();

int main ()
{
....
a();
....
}

int a()
{
....
b();
....
}

int b()
{
....
a();
....
}

=) sorry for my eng;)

Oct 25 '07 #1
12 1169
NoName schrieb:
I try it:

def b():
...
a()
...

def a():
...
b()
...

b()
it's not work.
It works.
def a():
print "a"
b()
def b():
print "b"
print a # not calling!
b()

But if you really call a in b, you create an endless loop. In all
programming languages, btw.

Is it possible pre-define function like in c++
No.
or place functions code
after main block?
Yes.
Oct 25 '07 #2
On Oct 25, 2:28 am, NoName <zaz...@gmail.comwrote:
I try it:

def b():
...
a()
...

def a():
...
b()
...

b()
it's not work.
It sure does. Please post full code and error message, something else
is wrong, not the cyclic reference.

George

Oct 25 '07 #3
On Thu, 25 Oct 2007 06:28:16 +0000, NoName wrote:
I try it:

def b():
...
a()
...

def a():
...
b()
...

b()
it's not work.
What do you mean by not working? At the time `b()` is called, both
functions are defined so it should working. Or at least it's not the
problem you think it is. The code above, the dots replaced with nothing,
will of course run "forever" until the stack limit is reached.

Ciao,
Marc 'BlackJack' Rintsch
Oct 25 '07 #4
On Oct 25, 7:28 am, NoName <zaz...@gmail.comwrote:
I try it:

def b():
...
a()
...

def a():
...
b()
...

b()
it's not work.
Probably all those dots!
Is it possible pre-define function like in c++ or place functions code
after main block?
Python binds names to objects dynamically: this means that when a() is
compiled, the "b()" line in its definition is compiled to something
that says "look for the object currently bound to the name 'b' in the
global dictionary, and execute the __call__ method of that object with
no arguments". It doesn't make any difference what object 'b' is
bound to at this time.

HTH

--
Arnaud
Oct 25 '07 #5
On Oct 25, 10:30 am, Arnaud Delobelle <arno...@googlemail.comwrote:
[...]
"look for the object currently bound to the name 'b' in the
global dictionary, and execute the __call__ method of that object with
no arguments"
This is what happens at runtime. Rereading, I thought I hadn't made
it clear.

--
Arnaud
Oct 25 '07 #6
sorry! Yes it's work.
What about 2 question?
Can i put function after main block?

print qq()

def qq():
return 'hello'

Traceback (most recent call last):
File "C:\Python25\projects\indexer\test.py", line 1, in <module>
print qq()
NameError: name 'qq' is not defined
Or onli possible:

def main():
print qq()

def qq():
return 'hello'

main()

Oct 25 '07 #7
NoName wrote:
sorry! Yes it's work.
What about 2 question?
Can i put function after main block?
print qq()

def qq():
return 'hello'
You can't call a thing before it is defined.
Traceback (most recent call last):
File "C:\Python25\projects\indexer\test.py", line 1, in <module>
print qq()
NameError: name 'qq' is not defined
Or onli possible:

def main():
print qq()

def qq():
return 'hello'

main()
Yes. That's the way to go.

Diez
Oct 25 '07 #8
NoName a écrit :
sorry! Yes it's work.
What about 2 question?
Can i put function after main block?

print qq()

def qq():
return 'hello'
Where's your "main block" here ?
Traceback (most recent call last):
File "C:\Python25\projects\indexer\test.py", line 1, in <module>
print qq()
NameError: name 'qq' is not defined
Indeed. When the code is loaded in the interpreter (that is, when passed
as a script or when imported as a module), all top-level statements are
executed sequentially. This creates all the function and class objects
and populate the module's namespace accordingly.
>
Or onli possible:

def main():
print qq()

def qq():
return 'hello'

main()
The canonical case for small scripts is to have first all functions and
globals defined, then the main code protected by a guard, ie:

import something

SOME_CONST = 42

def do_something():
pass

def try_something_else():
pass

if __name__ == '__main__':
print SOME_CONST
if not do_something():
try_somethin_else()
For bigger apps, you usually define all functions and classes in
modules, so the 'main' script doesn't define much - just do the needed
imports, and call the appropriate function or class.
Oct 25 '07 #9
On 2007-10-25, Bruno Desthuilliers
<br********************@wtf.websiteburo.oops.comwr ote:
The canonical case for small scripts is to have first all
functions and globals defined, then the main code protected by
a guard, ie:
There's no reason to "protect" your main code in a small script.
if __name__ == '__main__':
print SOME_CONST
if not do_something():
try_somethin_else()
That idiom is useful in modules for launching tests or examples
that should not be run when the module is imported.

--
Neil Cerutti
Oct 25 '07 #10
On 10/25/07, Neil Cerutti <ho*****@yahoo.comwrote:
On 2007-10-25, Bruno Desthuilliers
<br********************@wtf.websiteburo.oops.comwr ote:
The canonical case for small scripts is to have first all
functions and globals defined, then the main code protected by
a guard, ie:

There's no reason to "protect" your main code in a small script.
There's also not much reason not to, and it prevents disaster or at
least unwanted side effects when you accidentally run pychecker or
pydoc over it.

Also, debugging scripts is a lot easier when you can import them into a shell.
if __name__ == '__main__':
print SOME_CONST
if not do_something():
try_somethin_else()

That idiom is useful in modules for launching tests or examples
that should not be run when the module is imported.
I use it whenever there's any code I don't want run unless I'm
explicitly trying to do so.
Oct 25 '07 #11
Neil Cerutti a écrit :
On 2007-10-25, Bruno Desthuilliers
<br********************@wtf.websiteburo.oops.comwr ote:
>The canonical case for small scripts is to have first all
functions and globals defined, then the main code protected by
a guard, ie:

There's no reason to "protect" your main code in a small script.
I actually have at least one: it allow me to execute all the top-level
definitions in Emacs' Python sub-interpreter buffer for testing, without
actually executing the 'main' code.
>if __name__ == '__main__':
print SOME_CONST
if not do_something():
try_somethin_else()

That idiom is
<insert>also</insert>
useful in modules for launching tests or examples
that should not be run when the module is imported.
Indeed.
Oct 26 '07 #12
On 2007-10-26, Bruno Desthuilliers <br********************@wtf.websiteburo.oops.comwr ote:
Neil Cerutti a écrit :
>On 2007-10-25, Bruno Desthuilliers
<br********************@wtf.websiteburo.oops.comw rote:
>>The canonical case for small scripts is to have first all
functions and globals defined, then the main code protected by
a guard, ie:

There's no reason to "protect" your main code in a small
script.

I actually have at least one: it allow me to execute all the
top-level definitions in Emacs' Python sub-interpreter buffer
for testing, without actually executing the 'main' code.
>>if __name__ == '__main__':
print SOME_CONST
if not do_something():
try_somethin_else()

That idiom is

<insert>also</insert>
>useful in modules for launching tests or examples
that should not be run when the module is imported.
Indeed.
Thanks to you and Chris for giving me the tool-related
applications of that idiom. I haven't used the associated tools.

--
Neil Cerutti
Ushers will eat latecomers. --Church Bulletin Blooper
Oct 26 '07 #13

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
4
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at...
6
by: Sandman | last post by:
Ok, not sure how I should explain this, but I'll give it a try. When a browser requests any given page from my webapp, it include()'s lots of other smaller scripts that has functions that does...
14
by: Alan Silver | last post by:
Hello, I have spent ages trawling through Google, looking for information about global functions in ASP.NET and I'm still not clear about the best way to go about this (or not). I am writing...
4
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
2
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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.