[Zope] strange 'print' behavior in Python Scripts
Christopher N. Deckard
cnd@ecn.purdue.edu
Fri, 29 Jun 2001 14:58:53 -0500
Hmmm, ok. I see your point there. :-)
For rapidly throwing something together though, it is easy to triple
quote things and get it to work and then do it right. Guess I could
have just used DTML and change the content type.
Again, thanks for the advice. I'll try and do better about
seperating the templates out from the logic.
-Chris
Luciano Ramalho wrote:
>
> I suppose this has to do with the way PythonScripts are interpreted. As
> you may have noticed, Zope prepends a function definition line like "def
> your_id(args...):" to the text of your PythonScript before evaluating it
> (that is why errors in PythonScripts are reported with the line count
> off by one).
>
> In order to make the whole block syntatically valid, the body of the
> function must be indented relative to the function definition. So before
> evaluating your function, Zope must prepend a space in each line. It
> should be smart enough to treat triple-quotes differently, but it isn't.
>
> Now, I think the need to use triple-quoted strings can imply a design
> problem: it means you are embedding the template in your code, which is
> often a bad idea. Without knowing what you are doing, I would try and
> separate the embedded template from the Python logic.
>
> Regards,
>
> Luciano
>
> "Christopher N. Deckard" wrote:
> >
> > > From: "Christopher N. Deckard" <cnd@ecn.purdue.edu>
> > > > Clarification, this happens when using triple quotes
> > > >
> > > > print '''
> > > > foo
> > > > bar
> > > > '''
> > > >
> > > > Actually prints:
> > > > foo
> > > > bar
> > >
> > > Ah, I knew this would bite somebody someday, but you're the first to
> > > mention it. When a Script is compiled, it is actually converted into a
> > > function by prefixing it with a "def f(params..." line. If the body
> > > isn't indented, it won't compile, so it indents it one space. You can
> > > get around this by manually indenting. In other words:
> > >
> > > ## Script (Python) "indented"
> > > print '''
> > > foo
> > > bar
> > > '''
> > > return printed
> >
> > Hmm, this was not the answer I was looking for. Are there any
> > thoughts on fixing this in the future? I've fun into it many times
> > now and couldn't figure out what was going on. It is especially bad
> > when printing a textarea for a form. Everything comes out indented
> > a space.
> >
> > I'll just go line by line for now. Thanks for the answer.
> >
> > -Chris
> >
> > _______________________________________________
> > Zope maillist - Zope@zope.org
> > http://lists.zope.org/mailman/listinfo/zope
> > ** No cross posts or HTML encoding! **
> > (Related lists -
> > http://lists.zope.org/mailman/listinfo/zope-announce
> > http://lists.zope.org/mailman/listinfo/zope-dev )
>
> _______________________________________________
> Zope maillist - Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://lists.zope.org/mailman/listinfo/zope-announce
> http://lists.zope.org/mailman/listinfo/zope-dev )