Newbie's unsurmountable pb with IO stream trick

Steve Holden sholden at holdenweb.com
Sat Sep 7 11:54:34 EDT 2002


"Spendius" <spendius at muchomail.com> wrote ...
> Peter Hansen <peter at engcorp.com> wrote in message...
>
> OK I forgot to say I'm trying to develop my stuff under Windoze.
>
> > at once here.  For one thing, why are you trying to use
> > global variables at all when the original code was in
> > Java, which doesn't have such things?
> Do you mean Java doesn't have global variables or I'm mistaken ?!?
>
Java maintains a much stricter control over named access to non-local
variables than Python does. If you get an "UnboundLocalError" this
invaribaly means you are trying to use a variable which has not previously
been bound to a value. At the very least this makes your program logic
questionable.

> > And what does not declaring variables have to do with
> > the "global keyword issue" (whatever that is)?
> I kept getting the error message
> # UnboundLocalError: local variable 'cpt' referenced before assignment
> where I really didn't expect to get such an error, and noticed in this
> forum a lot of guys who know Python complain and talk themselves about
> a "mess" (my 'cpt' var. is a counter incremented every time a method
> gets called and I must sometimes re-initialize it to 0 inside an 'if'
> statement: merely IMPOSSIBLE).
>
Not at all. The problem is that, if you are using a global variable, you
must either initialize it before first use outside the functions that refer
to it, or you must ensure that your first call to any such function will
initialize it. This is simply sound programming practice.

> > If you have an SQL database to access, why not use one of the
> > standard DBAPI modules to get to it?  You might be doing a lot
> > of extra work when you don't have to.
> And why not ? Anyway SQL*Plus contains plenty of built-in commands
> that allow you to format your output how you like: I'd like to
> keep that functionality. And let's say that I want to do that for
> the sole purpose of learning the handling of IO streams in Python !
>
Well, that's a bit like saying you want to learn how to handle irregular
verbs in Esperanto, since the Java streams concept doesn't transliterate
directly into Python. You are simply bringing too much of your mental Java
baggage into the Python domain, where it will get in your way rather than
helping you to solve problems.

As far as inefficiency goes, sure, you may wish to solve your problem by
interacting with an external SQL/Plus process even if there are better ways
to go (and there are, as Peter mentioned). However, you should not expect
Java-style solutions to be appropriate in the Python world, where we have
popen() and friends to talk to external processes.

> > If you're just learning Python, you've taken on a fairly
> > complicated and large task right at the beginning.  Why not
> I admit it. But I did *exactly* the same with Java (writing
> the app I'm trying to port) but never got stuck into problems
> that blocking...
>
Don't try to port the Java, try to re-implement it in Python :-)

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                        pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list