Please hear my plea: print without softspace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Bless

    Please hear my plea: print without softspace


    Why can't we have an additional 'print' statement, that behaves
    exactly like 'print' but doesn't insert that damn blank between two
    arguments?

    Could be called 'printn' or 'prin1' or 'prinn' anything similar you
    like.

    Would make my life so much easier. Often I start with a nice and short
    print statement, then there happen to be more arguments and neat
    formatting becomes more important and as a result I find myself
    rewriting the code over and over again just to cope with those blanks
    I'm forced to take into account.

    Coding would be easier: If there are more arguments to print, it's
    just the argument list that grows. If you need a blank then add one.
    But you don't have to rewrite your arguments into expressions which
    can be tedious and isn't efficient anyway if its an extra operation
    just to get rid of those blanks.

    What do you think?

    Martin
  • Carmine Noviello

    #2
    Re: Please hear my plea: print without softspace

    def pprint(*args):
    txt = ""
    for a in args:
    txt += str(a)
    print txt

    Is it ok?

    --
    Don't you know why your Python application has crashed?
    Take a look to http://www.pycrash.org


    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

    Comment

    • Robin Becker

      #3
      Re: Please hear my plea: print without softspace

      In article <dca1dc79fa2d9e fd5bb586b079d4d b53.46955@mygat e.mailgate.org> ,
      Carmine Noviello <cnoviello@hotm ail.com> writes[color=blue]
      >def pprint(*args):
      > txt = ""
      > for a in args:
      > txt += str(a)
      > print txt
      >
      >Is it ok?
      >
      >--
      >Don't you know why your Python application has crashed?
      >Take a look to http://www.pycrash.org
      >
      >[/color]
      Give stdout a good hiding :)

      ##############
      class hidesoftspace:
      def __init__(self,f obj):
      self.__dict__['_fobj'] = fobj
      fobj.softspace = False
      def __getattr__(sel f,a):
      if a=='softspace': return False
      return getattr(self._f obj,a)
      def __setattr__(sel f,a,v):
      if a=='softspace': return
      setattr(self._f obj,a,v)

      import sys
      print 'before hiding',1,2,3,4
      sys.stdout=hide softspace(sys.s tdout)
      print 'after hiding',1,2,3,4
      ##############
      results in

      before hiding 1 2 3 4
      after hiding1234
      --
      Robin Becker

      Comment

      • Andrei

        #4
        Re: Please hear my plea: print without softspace

        Martin Bless wrote on Sat, 28 Feb 2004 11:56:55 GMT:
        [color=blue]
        > Why can't we have an additional 'print' statement, that behaves
        > exactly like 'print' but doesn't insert that damn blank between two
        > arguments?
        >
        > Could be called 'printn' or 'prin1' or 'prinn' anything similar you
        > like.[/color]
        <snip>[color=blue]
        > What do you think?[/color]

        In these cases I tend to use string formatting, e.g.:

        print '%s-%s-%s' % (1,2,3)

        As others have already pointed out, there are several other ways too, so
        making a new keyword for this doesn't seem very useful.

        --
        Yours,

        Andrei

        =====
        Real contact info (decode with rot13):
        cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
        gur yvfg, fb gurer'f ab arrq gb PP.

        Comment

        • Valentino Volonghi aka Dialtone

          #5
          Re: Please hear my plea: print without softspace

          In data Sat, 28 Feb 2004 12:01:40 +0000 (UTC), Carmine Noviello ha
          scritto:
          [color=blue]
          > def pprint(*args):
          > txt = ""
          > for a in args:
          > txt += str(a)
          > print txt
          >
          > Is it ok?[/color]

          I would use this instead:

          def pprint(*args):
          txt = []
          for a in args:
          txt.append(a)
          print ''.join(txt)

          which is a lot faster :)

          --
          Valentino Volonghi aka Dialtone
          Now using Windows for Notebook Hw failure
          X Python Newsreader developer
          Download XPN for free. XPN is a multiplatform newsreader with unicode support. It's written in Python using GTK+2 toolkit and works wherever Python and GTK+2 work.

          Comment

          • Mel Wilson

            #6
            Re: Please hear my plea: print without softspace

            In article <9nrc43idsjh1.7 af9ozuv2nc7$.dl g@40tude.net>,
            Valentino Volonghi aka Dialtone <dialton3.NOSPA M_@virgilio.it> wrote:[color=blue]
            >In data Sat, 28 Feb 2004 12:01:40 +0000 (UTC), Carmine Noviello ha
            >scritto:
            >[color=green]
            >> def pprint(*args):
            >> txt = ""
            >> for a in args:
            >> txt += str(a)
            >> print txt
            >>
            >> Is it ok?[/color]
            >
            >I would use this instead:
            >
            >def pprint(*args):
            > txt = []
            > for a in args:
            > txt.append(a)
            > print ''.join(txt)
            >
            >which is a lot faster :)[/color]

            Sorry.

            Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
            Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
            >>> def pprint(*args):[/color][/color][/color]
            .... txt = []
            .... for a in args:
            .... txt.append(a)
            .... print ''.join(txt)
            ....[color=blue][color=green][color=darkred]
            >>> pprint (1,2,3)[/color][/color][/color]
            Traceback (most recent call last):
            File "<stdin>", line 1, in ?
            File "<stdin>", line 5, in pprint
            TypeError: sequence item 0: expected string, int found



            def pprint (*args):
            print ''.join ([str(x) for x in args])



            Regards. Mel.

            Comment

            • kosh

              #7
              Re: Please hear my plea: print without softspace

              On Saturday 28 February 2004 07:32 am, Valentino Volonghi aka Dialtone wrote:[color=blue]
              > In data Sat, 28 Feb 2004 12:01:40 +0000 (UTC), Carmine Noviello ha
              >
              > scritto:[color=green]
              > > def pprint(*args):
              > > txt = ""
              > > for a in args:
              > > txt += str(a)
              > > print txt
              > >
              > > Is it ok?[/color]
              >
              > I would use this instead:
              >
              > def pprint(*args):
              > txt = []
              > for a in args:
              > txt.append(a)
              > print ''.join(txt)
              >
              > which is a lot faster :)[/color]

              why not?

              def pprint(*args):
              print ''.join(args)

              if you want it to be more robust you could do

              def pprint(*args):
              print ''.join(map(str ,args))

              That would try and convert all the items to a string first before joining
              them.

              Comment

              • Nick Patavalis

                #8
                Re: Please hear my plea: print without softspace

                In article <edc2QLAGMJQAFw oM@jessikat.fsn et.co.uk>, Robin Becker wrote:[color=blue]
                >
                >
                > ##############
                > class hidesoftspace:
                > def __init__(self,f obj):
                > self.__dict__['_fobj'] = fobj
                > fobj.softspace = False
                > def __getattr__(sel f,a):
                > if a=='softspace': return False
                > return getattr(self._f obj,a)
                > def __setattr__(sel f,a,v):
                > if a=='softspace': return
                > setattr(self._f obj,a,v)
                >
                > import sys
                > print 'before hiding',1,2,3,4
                > sys.stdout=hide softspace(sys.s tdout)
                > print 'after hiding',1,2,3,4
                > ##############[/color]

                Would you perhaps care to comment a bit more, or provide a pointer to
                some docs? It seems that there is a "softspace" atribute in the
                sys.stdout file object, and that this attribute gets set by the print
                statement itself. With your clever trick you make it imposible for
                someone to set this attribute to anything else but "False". What are
                the exact semantics behind this?

                I tried this:

                import sys
                print sys.stdout.soft space
                0
                print "test:", 1, 2, 3, sys.stdout.soft space
                test: 1 2 3 1
                print sys.stdout.soft space
                0

                Which seems to support my explanation, but what exactly are the
                semantics of "softspace" ?

                Thanks
                /npat


                Comment

                • Valentino Volonghi aka Dialtone

                  #9
                  Re: Please hear my plea: print without softspace

                  In data Sat, 28 Feb 2004 09:44:57 -0700, kosh ha scritto:
                  [color=blue]
                  > why not?
                  >
                  > def pprint(*args):
                  > print ''.join(args)
                  >
                  > if you want it to be more robust you could do
                  >
                  > def pprint(*args):
                  > print ''.join(map(str ,args))
                  >
                  > That would try and convert all the items to a string first before joining
                  > them.[/color]

                  I would have used the list comprehension solution that Mel Wilson posted
                  but I was in a hurry and wrote wrong code...

                  Actually I don't like map() and I always try to use LC as much as
                  possible, I think they are a lot more readable.

                  --
                  Valentino Volonghi aka Dialtone
                  Now using Windows for Notebook Hw failure
                  X Python Newsreader developer
                  Download XPN for free. XPN is a multiplatform newsreader with unicode support. It's written in Python using GTK+2 toolkit and works wherever Python and GTK+2 work.

                  Comment

                  • Robin Becker

                    #10
                    Re: Please hear my plea: print without softspace

                    In article <slrnc41i7e.81f .npat@localhost .localdomain>, Nick Patavalis
                    <npat@efault.ne t> writes[color=blue]
                    >In article <edc2QLAGMJQAFw oM@jessikat.fsn et.co.uk>, Robin Becker wrote:[color=green]
                    >>
                    >>
                    >> ##############
                    >> class hidesoftspace:
                    >> def __init__(self,f obj):
                    >> self.__dict__['_fobj'] = fobj
                    >> fobj.softspace = False
                    >> def __getattr__(sel f,a):
                    >> if a=='softspace': return False
                    >> return getattr(self._f obj,a)
                    >> def __setattr__(sel f,a,v):
                    >> if a=='softspace': return
                    >> setattr(self._f obj,a,v)
                    >>
                    >> import sys
                    >> print 'before hiding',1,2,3,4
                    >> sys.stdout=hide softspace(sys.s tdout)
                    >> print 'after hiding',1,2,3,4
                    >> ##############[/color]
                    >[/color]

                    [color=blue]
                    >Would you perhaps care to comment a bit more, or provide a pointer to
                    >some docs? It seems that there is a "softspace" atribute in the
                    >sys.stdout file object, and that this attribute gets set by the print
                    >statement itself. With your clever trick you make it imposible for
                    >someone to set this attribute to anything else but "False". What are
                    >the exact semantics behind this?
                    >[/color]
                    [color=blue]
                    >I tried this:
                    >
                    > import sys
                    > print sys.stdout.soft space
                    > 0
                    > print "test:", 1, 2, 3, sys.stdout.soft space
                    > test: 1 2 3 1
                    > print sys.stdout.soft space
                    > 0
                    >
                    >Which seems to support my explanation, but what exactly are the
                    >semantics of "softspace" ?
                    >
                    >Thanks
                    >/npat
                    >
                    >[/color]

                    OK I'll try. I believe softspace is a boolean attribute of the standard
                    file class which is used by print to indicate that a space should be
                    inserted in the stream before the next item. Logically it should always
                    be false at the beginning of a print sequence and true after the print
                    of an item.

                    You have to be careful testing this as prints at the prompt don't behave
                    properly ie you always get a linefeed in the command terminal.

                    try
                    from sys import stdout
                    print stdout.softspac e,stdout.softsp ace,;print stdout.softspac e

                    should give

                    0 1 1

                    my class just ensures that a wrapped (warped :)) file will never have
                    its softspace set to true and thus we won't get the extra spaces; in
                    short print always thinks the wrapped file is at the start of a print
                    sequence.
                    -softspaced in the head-ly yrs-
                    Robin Becker

                    Comment

                    • kosh

                      #11
                      Re: Please hear my plea: print without softspace

                      On Saturday 28 February 2004 10:26 am, Valentino Volonghi aka Dialtone wrote:[color=blue]
                      > In data Sat, 28 Feb 2004 09:44:57 -0700, kosh ha scritto:[color=green]
                      > > why not?
                      > >
                      > > def pprint(*args):
                      > > print ''.join(args)
                      > >
                      > > if you want it to be more robust you could do
                      > >
                      > > def pprint(*args):
                      > > print ''.join(map(str ,args))
                      > >
                      > > That would try and convert all the items to a string first before joining
                      > > them.[/color]
                      >
                      > I would have used the list comprehension solution that Mel Wilson posted
                      > but I was in a hurry and wrote wrong code...
                      >
                      > Actually I don't like map() and I always try to use LC as much as
                      > possible, I think they are a lot more readable.[/color]

                      I use map, reduce, filter etc pretty rarely but I think in this case it is a
                      very simple way to say what I mean there. I just want str called on every
                      element in args and have a list returned with that result.

                      I use list comps a lot in other places but things like map do have their
                      place.

                      Comment

                      • Paul Rubin

                        #12
                        Re: Please hear my plea: print without softspace

                        "Carmine Noviello" <cnoviello@hotm ail.com> writes:[color=blue]
                        > def pprint(*args):
                        > txt = ""
                        > for a in args:
                        > txt += str(a)
                        > print txt
                        >
                        > Is it ok?[/color]

                        import sys
                        def pprint(*args):
                        for a in args:
                        sys.stdout.writ e(a)

                        Comment

                        • Terry Reedy

                          #13
                          Re: Please hear my plea: print without softspace


                          "Martin Bless" <mb@muenster.de > wrote in message
                          news:4041796c.2 181078@news.mue nster.de...[color=blue]
                          >
                          > Why can't we have an additional 'print' statement, that behaves
                          > exactly like 'print' but doesn't insert that damn blank between two
                          > arguments?[/color]
                          ....[color=blue]
                          > What do you think?[/color]

                          For exact control of output, use file.write(stri ng).
                          print is a convenient wrapper for casual output to stdout,
                          especially with the interactive console.
                          Or write your own wrapper for custom default formating.

                          Terry J. Reedy




                          Comment

                          • Dennis Lee Bieber

                            #14
                            Re: Please hear my plea: print without softspace

                            On Sat, 28 Feb 2004 11:56:55 GMT, mb@muenster.de (Martin Bless)
                            declaimed the following in comp.lang.pytho n:
                            [color=blue]
                            >
                            > Why can't we have an additional 'print' statement, that behaves
                            > exactly like 'print' but doesn't insert that damn blank between two
                            > arguments?[/color]

                            Because we'd then have newbies complaining because they can't
                            tell different outputs apart when they use the wrong "print" statement
                            <G>
                            [color=blue]
                            > Would make my life so much easier. Often I start with a nice and short[/color]
                            <snip>[color=blue]
                            > can be tedious and isn't efficient anyway if its an extra operation
                            > just to get rid of those blanks.
                            >
                            > What do you think?
                            >[/color]
                            /I/ think "print" should be reserved for quick and simple,
                            interactive prompting style, I/O operations, and anything that may
                            require custom, exact, formatting should be performed using
                            sys.stdout.writ e() and related... possibly with dedicated
                            functions/classes/modules (a report writer, say)


                            --[color=blue]
                            > =============== =============== =============== =============== == <
                            > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                            > wulfraed@dm.net | Bestiaria Support Staff <
                            > =============== =============== =============== =============== == <
                            > Home Page: <http://www.dm.net/~wulfraed/> <
                            > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                            Comment

                            • Martin Bless

                              #15
                              Again: Please hear my plea: print without softspace

                              [Andrei <fake@fake.ne t>]
                              [color=blue]
                              >In these cases I tend to use string formatting, e.g.:
                              >
                              >print '%s-%s-%s' % (1,2,3)
                              >
                              >As others have already pointed out, there are several other ways too, so
                              >making a new keyword for this doesn't seem very useful.[/color]

                              That's exactly the point I'm trying to make: I think this new keyword
                              *would* be extremely useful.

                              The general advice I received so far is: "roll your own writer object,
                              create a wrapper, do those string calculations yourself, then hand the
                              ready to print output to 'print' or some 'write' method. 'print'
                              isn't good enough for sophisticated use, there are many ways to format
                              your output correctly..."

                              I *know* there are many ways. The point is: many of these
                              "workaround s" only come into play because there *is no* built in
                              variant of 'print' that doesn't emit blanks.

                              Advocating the 'print statement':

                              Let's remember:

                              print_stmt ::= "print" ( [expression ("," expression)* [","]]
                              | ">>" expression [("," expression)+ [","]] )

                              - Obviously it *is* an important language feature: it even has its own
                              keyword.
                              - It's easy to use and *scalable* (no parentheses, variable number of
                              arguments).
                              - readable: pure list of arguments.
                              - Its efficient, since it has its built in ways of transforming
                              expressions to output strings, processing one by one. No need to join
                              those parts first.
                              - As a language feature it's well documented.
                              - 'print statements' stand for themselves and are highly expressive
                              and self documenting. Understanding, copying and pasting relies less
                              on context.

                              In one word: Looks like a very pythonic feature to me. Very
                              attractive.

                              And I'd simply like to benefit from all of these advantages. 99% seem
                              to be ok and because of 1% bad we can't use it to the full.

                              Aside: And there even must have been a time where 'print' has been
                              considered important enough to fight a long battle about introducing
                              the "chevron" form ('>>')...

                              Don't panic:

                              Of course I'm not saying we should change the existing behaviour of
                              'print' in any way.

                              But:

                              On the other hand: introducing a new keyword like 'printn' or 'prin0'

                              - wouldn't break any existing code
                              - wouldn't introduce a change of language structure
                              - could be documented within a few sentences ("... behaves like
                              'print' but does not insert blanks ...")
                              - and my suspicion is that the implentation in C isn't much of a
                              problem.

                              It's just like having a variant of something we allready have with
                              slightly different semantics. And that's ok and exactly what we want.
                              And it's no problem because we'd have a unique new name for it.

                              As far as I can see there's much benefit and little costs.

                              Ok, then back to where I startet. Let me asked again:

                              "Can we have an additional 'print' statement, that behaves exactly
                              like 'print' but doesn't insert blanks between arguments?"

                              curious,

                              Martin

                              Comment

                              Working...