Test for structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Reedy

    #16
    Re: Test for structure


    "Steven Bethard" <steven.bethard @gmail.com> wrote in message
    news:GvKdnbutV6 lQt4ffRVn-jA@comcast.com. ..[color=blue]
    > I don't like this idea much because it depends on str and unicode _not_
    > having a particular function. I haven't seen any guarantee anywhere that
    > str or unicode won't ever grow an __iter__ method. So this code seems
    > dangerous as far as future compatibility goes.[/color]

    When CPython's support for the old iteration protocol goes away, which I
    expects it will someday, strings will have to grow an __iter__ method.
    Even now, I think this difference between strings and lists is more of an
    accident than a logical design. So the test is opaque and specific to
    current CPython. The validity of something like a = a+'', however, is
    inherent to the nature of strings.

    Terry J. Reedy



    Comment

    • Martin Miller

      #17
      Re: Test for structure

      At the end of his last post, Steve Bethard wrote:[color=blue]
      > That said, I find that in most cases, the better option is to use[/color]
      *args[color=blue]
      > in the original function though. For example:
      >
      > def f(arg):
      > args = aslist(arg)
      > ...
      > f(42)
      > f(['spam', 'eggs', 'ham'])
      >
      > could probably be more easily written as:
      >
      > def f(*args):
      > ...
      > f(42)
      > f('spam', 'eggs', 'ham')
      >
      > Of course this won't work if you have multiple list arguments.[/color]

      Very interesting, but it also doesn't let you specify a default
      argument value...however this gave me the idea that it would be
      possible to use the *args idea to greatly simplify the proposed
      aslist() function -- when one was needed to allow default argument
      values and/or for handling multiple list arguments. Namely:

      def aslist(*args):
      return list(args)

      def f(arg=None):
      args = aslist(arg)
      ...

      f()
      f(42)
      f('tanstaafl')
      f(['spam', 'eggs', 'ham'])

      This seems fairly lean and mean, with no if, isinstance, hasattr, or
      try/excepts required -- although aslist() might need a check for the
      single argument of None case, depending on whether it should return []
      or something besides [None] in that situation.

      Best,
      Martin

      Comment

      • Martin Miller

        #18
        Re: Test for structure

        Ooops. I left out an "*" on a statement in the new aslist() function. I
        should have written:

        def aslist(*args):
        return list(*args) # corrected

        def f(arg):
        args = aslist(arg)
        ...

        Sorry,
        Martin

        Comment

        • Martin Miller

          #19
          Re: Test for structure

          Nope, that isn't right either, in the sense that it handles all the
          cases properly, including "single string" vs "list of strings'. Guess
          this overly simplistic aslist() does not work after. I should have been
          more suspicious and cautious before posting. Sorry.

          Martin

          Comment

          Working...