argument type

It's me itsme at yahoo.com
Tue Dec 28 01:11:48 EST 2004


"Steven Bethard" <steven.bethard at gmail.com> wrote in message
news:2w6Ad.576350$wV.27320 at attbi_s54...

<snip>

>
> Yeah, given those constraints, you basically have two options -- check
> for list/tuple (as you have above) or check for str/unicode (as I do
> below).  I personally prefer the latter because it seems much more
> likely that users will define alternate collection classes than that
> users will define alternate string classes.  My code would look like:
>
>      def abc(arg1, arg2, arg3):
>          if not isinstance(arg2, basestring):
>              for item in arg2:
>                  abc(arg1, item, arg3)
>          else:
>              # do whatever with arg2 (guaranteed to be a string)
>
> That way if you later call your code with, say:
>
>      abc(1, set(["String 1", "String 2"]), 5)
>
> or
>
>      abc(1, collections.deque(["String 1", "String 2"]), 5)
>
> it still works.
>

Of course.  For now, it's okay because I haven't learn about set and other
types yet.   I am still thinking in very elementary terms.

I need to look up and see what:

          if not isinstance(arg2, basestring):

does.


>
> Steve
>
>
> P.S.  My real preference here would be to change the order of your
> arguments and write the function as:
>
>      def abc(arg1, arg3, *arg2s)
>          for arg2 in arg2s:
>              # do whatever with arg2 (should be a string)
>
> and then write your function calls as:
>
>       abc(1, 5, "String 1")
>       abc(1, 5, "String 1", "String 2")
>
> Then you don't ever have to remember []s. ;-)  But if you really are
> stuck with arg2 as the second argument, I'd probably go for testing
> isinstance(x, basestring).
>

No, that was just an example.   I actually have additional arguments that
are similar to arg2.  It's not like I can do:

      def abc(arg1, arg3, *arg2s, *arg3s, *arg4s)

:-)


> Steve





More information about the Python-list mailing list