473,249 Members | 1,805 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,249 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 1165
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...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

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.