[Tutor] function declaration problems perhaps?

Bob Gailer bgailer at alum.rpi.edu
Wed Jul 25 18:19:51 CEST 2007


Alan Gauld wrote:
> "nibudh" <nibudh at gmail.com> wrote
>
>   
>> in perl this works:
>>
>> #!/usr/bin/env perl
>> hello("World");
>>
>> sub hello {
>>    print "Hello ". $_[0] . "\n";
>> }
>>     
>
>
> Perl executes differently to Python in that it does a compilation 
> stage
> before executing. Therefore Perl knows about all the function 
> definitions
> prior to executing any code. Python compiles modules which it imports
> but not scripts which it executes.
>   
Not exactly. When Python imports a module that is new* it "compiles" it 
into bytecode. No recognition of names or objects takes place in this 
step. The bytecode is saved in a file with extension .pyc. Then Python 
executes the bytecode. Any function definitions that get executed create 
function objects that are available to subsequently executed code.

Running a script does exactly the same thing, except the bytecode is not 
saved in a file.

The bottom line is: a function definition must be executed before the 
function can be used. This is true of ANY Python object.

*new means that no .pyc file exists or the modification time of the .py 
is more recent than that of the .pyc.
>   
>> I have a vague recollection that ASP works in a similar way to 
>> python hence
>> the "hunch" i had earlier but i could be wrong. It's been a while 
>> since i've
>> done programming.
>>     
>
> Most interpreted languages work this way.
> Even the original versions of C worked that way although I thiunk more
> recent (ANSI/ISO compliant?) versions no longer need the strict 
> ordering,
> and Pascal also does it that way even though they are pure compiled
> languages. In the case of Pascal it is because Pascal is designed to
> be a single pass comilation language - which is why Borland's Object
> Pascal comiles so quickly in Delphi!
>
>   


-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC




More information about the Tutor mailing list