3

numberofrow, its value is dynamically set in form field.

now since numberofrow is in multiple tables, when i receive that variable from form,

if only one numerofrow, its a String, for ex. numberofrow = 01

if more than one numberofrow, its a list, for ex. numberofrow = [01, 02, 04]

Now how do i differentiate if its a list or string in my python code?

am thinking of using,

if type(numberofrow).__name__=='list':
    #do this
else:
    #do this

Thanks, Sunny.

sunny
  • 31
  • 2
  • 3
  • 2
    Duplicate of http://stackoverflow.com/questions/3227552/what-is-the-pythonic-way-of-differentiating-between-a-string-and-a-list ? – johnsyweb Aug 18 '10 at 05:37

3 Answers3

6

For this purpose there is a build-in called isinstance. You can use it to check if an object is an instance of that class (and compared to your solution also super classes are considered in this test).

if isinstance(numberofrow, list):
    # do this
else:
    # do that

It's quite common to do something like isinstance(numberofrow, basestring). basestring is the super class of both string types in Python 2 - str and unicode - and the test will match both of them.

Alternatively, you can also provide a tuple of possible classes/types like isinstance(numberofrow, (list, tuple)). This test will succeed if the instance is either a instance of a list or a tuple.

tux21b
  • 90,183
  • 16
  • 117
  • 101
4

What framework are you using to get that value from the form? A sensible one would definitely provide a way to return always a list -- specifically, if the value has been entered just once, a one-item list (and possibly, if the value has not been entered at all, an empty list).

For example, with good old cgi, you'd use the getlist method of the FieldStorage instance -- form.getlist('numberofrow') instead of form.getvalue('numberofrow') which behaves as you describe, returning either a string or a list -- and that would solve all your problems much more simply and elegantly!

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
1

You can do what you said, but a better way would be:

if isinstance(numberofrow, list):
    # do this
else:
    # do that

An alternative is to use the object as a string and change behavior when something goes wrong:

try:
    validated = numberofrow.isdigit()
except AttributeError:
    # must be a list?
    # do something else
else:
    # must be a string
    # do the string thing

If it acts like a string then it doesn't matter if it is a string or not.

Eric Snow
  • 1,198
  • 8
  • 21
  • since the code is quite big, is it possible, if numberofrow is string convert it into a list, with same variable name, as numberofrow, and then do rest of coding? – sunny Aug 18 '10 at 05:37
  • @sunny, that is a good idea. It's kind of what Alex is talking about with the benefit of the data always being a list. So, like you said, if it's a string, turn it into a list. You have be be careful since strings act like lists in many ways (both are sequences and have that interface). – Eric Snow Aug 18 '10 at 05:55
  • @ Eric, i got it working this way. @Alex, am using plone framework. Thanks all for the solutions. – sunny Aug 18 '10 at 06:02