Re: How to detect list versus string

Discussion in 'Python' started by Donn Cave, Jun 12, 2004.

  1. Donn Cave

    Donn Cave Guest

    Quoth Jonathon McKitrick <>:

    | I have a method that builds a dynamic combo box. Before I do that, I set a
    | class variable to the new list of items.
    |
    | def make_new_cat_box(self, cats):
    | self.cat_list = cats
    |
    | Sounds simple. But sometimes, my combo box will only have one choice
    | available. When I call this method with a list of one string, the string is
    | split up, and my combo box now has a separate item for each letter in the
    | string. What I obviously want to do is detect when the object coming in is
    | a list or a string. type() isn't as useful as I had hoped, and len() will
    | give me the length of the string, so I cannot tell if it is a string or a
    | list of more that one item.

    Well, there are a variety of approaches. Some programmers like to try
    to discriminate on the basis of functional distinctions, so for example
    they'll try to add two quantities together to see if their types are
    compatible in that respect. Since you apparently don't get tuples, only
    lists, you could test for hasattr(cats, 'append'). Etc.

    But what's going on there, really? `When I call this method with a list
    of one string ...?' Like this, a.make_new_cat_box([v])? Why would that
    put you in this spot?

    It sounds like you're really calling this method with just a string, not
    a list of one string. In which case, if you can avoid that, please do,
    put the string in a list and consider the problem solved!

    Donn Cave,
    Donn Cave, Jun 12, 2004
    #1
    1. Advertising

  2. Donn Cave

    j_mckitrick Guest

    "Donn Cave" <> wrote in message
    > It sounds like you're really calling this method with just a string, not
    > a list of one string. In which case, if you can avoid that, please do,
    > put the string in a list and consider the problem solved!


    Hi Donn,

    It turns out that's exactly what I did. When I know that I am sending
    only one string for the combo box, I just send it as [CATEGORY] and
    that solves it! I'm still learning the 'zen' of Python. :)

    Thanks!

    jonathon
    j_mckitrick, Jun 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,879
    Scott M.
    Oct 11, 2003
  2. Matt
    Replies:
    3
    Views:
    479
    Tor Iver Wilhelmsen
    Sep 17, 2004
  3. Jonathon McKitrick

    How to detect list versus string

    Jonathon McKitrick, Jun 11, 2004, in forum: Python
    Replies:
    1
    Views:
    267
    Peter Otten
    Jun 11, 2004
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    240
    Christoffer Sawicki
    Sep 2, 2006
  5. Paul Butcher
    Replies:
    12
    Views:
    690
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page