[Tutor] Expression order problem

William O'Higgins william.ohiggins at utoronto.ca
Thu May 26 18:16:23 CEST 2005


I am running into problems with script evaluation order - specifically,
the Python interpreter seems to read and execute scripts line by line.
This is a problem if you are used to Perl, where the whole script is
parsed first (Perl also auto-vivifies variables, but that's a different
problem for a different day).  Here is a test script:

#!/usr/bin/python

import sys

class test:
    variable = "is this a variable" + other_argument
        
if sys.argv[1:]:
    argument = sys.argv[1]
    other_argument = sys.argv[2]
    instance = test()
    print instance.variable
else:
    sys.exit()

I am only beginning to get my head around OO programming, so please bear
with me.  In Perl, I would define all of my subs (functions or classes)
at the end of the program, because the Perl parser will read the whole
program, and then execute it.  So I'm having trouble from the start
because I can't do that - the interpreter whines about undefined calls
when things are defined further down the page.  In my example though - I
want to use variables from one code block in another, but whichever one
is first, something is undefined.  I'm not necessarily looking for a
fix, but pointers as to technique to avoid this would be appreciated.
Thanks.
-- 

yours,

William

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.python.org/pipermail/tutor/attachments/20050526/2eb4dc32/attachment.pgp


More information about the Tutor mailing list