New to Python

J. Cliff Dyer jcd at sdf.lonestar.org
Wed Feb 10 16:57:09 EST 2010


On Wed, 2010-02-10 at 13:18 -0800, Stephen Hansen wrote:

> 
> The original code:
> 
> 
> s = f.readline()
> if 'mystring' in s: print 'foundit'
> if 'mystring' not in s: print 'not found'
> if 'mystring' in s:
>   print 'processing'
> 
> 
> ... will only work on Python 2.x, as print is being used as a
> statement. If you change print to a function, that code will work in
> either Python 2.x or Python 3.x.
> 
> 
> However, in both CPython and IronPython, the above is provably correct
> code. It works fine: with those five lines alone, you are guaranteed
> to get 'foundit' followed by 'processing' if mystring is in s. With
> those five lines alone, you must get that result. If you aren't, then
> there's something else going on before or after-- and its not about
> IronPython vs CPython. To help diagnose what's wrong, copy and paste
> -real- results from a command prompt or interpreter when you run it,
> providing complete code. Something else is going wrong, you're looking
> in the wrong place to find the solution.

One other assumption that you are making is that f is a file-like object
that supports a reasonalbe readline function.

Quin: can you Ctrl-C, Ctrl-V the following code into a python file, by
itself, and run it to reproduce your problem?

from StringIO import StringIO
f = StringIO('This string contains mystring')
s = f.readline()
if 'mystring' in s: print 'foundit'
if 'mystring' not in s: print 'not found'
if 'mystring' in s:
   print 'processing'

# Should print:
# foundit
# processing
f = StringIO('This string does not contain MyStRiNg')
s = f.readline()
if 'mystring' in s: print 'foundit'
if 'mystring' not in s: print 'not found'
if 'mystring' in s:
   print 'processing'
# Should print:
# not found

And please, humor me and copy and paste the exact results as returned by
your IronPython interpreter. 






More information about the Python-list mailing list