[Discuss] A question for the Python gurus
Adam Parkin
pzelnip at telus.net
Sun Jul 30 09:40:27 PDT 2006
Okay before the replies, can someone explain to me this:
#!/usr/bin/python
import sys
lines = "hello"
def foo():
lines = sys.stdin.readline()
def bar():
print "in bar: " + lines
foo()
bar()
As I get "hello" as output. This is like having globals passed to
functions by value, which seems completely bizarre and
counter-intutitive. And it's not just strings: you get the same
semantics if "lines" is a hash, integer, etc.
Or am I just missing something?
Brian Quinlan wrote:
> You get an infinite loop, as you said. Then you exit the program and fix
> it. If you are prone to this type of typo, use PyChecker or some other
> type of linting tool.
Cool, I'd never heard of PyChecker before, thanks for the tip. And it
did point me in the direction of the problem with my (contrived)
infinite loop example, if that code wasn't run. That is, because
pychecker imports the script being tested, if you have the infinite loop
code getting called as a result of the import then you're hooped as the
only way to stop the infinite loop code is to hit CTRL+C which borks
pychecker along with the program being checked. So for example:
#!/usr/bin/python
import sys
def foo ():
line = sys.stdin.readline()
while (line):
print "you entered '" + line + "'"
lines = sys.stdin.readline()
foo()
Causes pychecker to end up in the same infinite loop, but if you remove
(or comment out) the call to foo then it works fine and gives you the
"unused variable lines" warning.
>> That actually raises another thing I've often wondered about Python,
>> is there any Python equivalent to the "use strict" statement in Perl
>> which forces you to declare a variable with "my" before using it?
>
> No, no such thing exists in Python.
Pity, as that can be a real time-saver in Perl (it's such an easy
mistake to make particularly in case-sensitive languages).
>> Why is there no eof() method in the file object class in Python?
>
> That's in interesting question. Probably because no one has ever needed
> one. You certainly don't in this case :-)
Well, you also don't need a lot of other coding constructs either, but
that doesn't mean that we don't have them in languages as syntactic
sugar to make our code nicer. =8-p
Deryk Barker wrote:
>> Ooops, misspelled a variable and because Python doesn't force you to
>> declare a variable you now have an infinite loop (I've actually done
>> something like this in Perl before I discovered "use strict").
>
> This is always a danger, writing loops this way doesn't make it any more
> or less dangerous.
Right, but that's why I'm saying that repeating code (even one line) is
even more of a bad thing than in other languages where you do have to
declare a variable upon first use as it's more likely you'll make a
mistake that won't be caught by the interpreter.
I'll give you the example's somewhat contrived, but the point is still
the same, you're duplicating code (which is a bad thing) or writing
poorly structured code (which is also a bad thing) simply because of a
language limitation.
> There seems to have been considerable talk of efficiency in this thread.
>
> If efficiency is that important, why are you using python (or any other
> interpreted language)?
Because I wanted to learn Python. =8-p I see your point (since Python
is a scripted/interpreted language you're going to be paying a
performance penalty no matter what), but that doesn't mean that
effeciency shouldn't be considered at all.
And I'm not saying that Guido and the Python maintainers don't consider
effeciency important, all I was saying was that the immutability of
strings seems to impose a rather severe performance penalty if all
you're doing is reading lines from a file, making a few changes to that
line and printing out the changed text (which is a common
text-processing task that scripting languages are supposed to be ideal for).
--
Adam Parkin
E-mail: pzelnip at telus.net
----------------------
Occasionally, the irresponsibility of software developers is comparable
to driving while very drunk.
-- Sara Baase, "A Gift of Fire", 2nd Ed, p136
More information about the Discuss
mailing list