?determine if x is a string or array in PythonScript
Hi I must be stupid or something, but I can't for the life of me work out a simple way of determining if a variable contains a string or array, in a PythonScript in Zope. I can't import type and or use type() function. isinstance doesn't work because I can't give a type as the second arg. I obviously just can't see the wood for the trees, can anyone help out this silly individual ? Rgds Tim
Tim Hoffman wrote:
Hi
I must be stupid or something, but I can't for the life of me work out a simple way of determining if a variable contains a string or array, in a PythonScript in Zope.
I can't import type and or use type() function. isinstance doesn't work because I can't give a type as the second arg.
Python scripts provide a special function, "same_type()", for this purpose. Example: if same_type(s, ''): s = [s] Shane
On Thu, Jul 11, 2002 at 04:10:33PM -0400, Shane Hathaway wrote:
Python scripts provide a special function, "same_type()", for this purpose. Example:
if same_type(s, ''): s = [s]
Much better than my hacked-up solution. :p -- Martijn Pieters | Software Engineer mailto:mj@zope.com | Zope Corporation http://www.zope.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
Thanks shane I missed that in the doc's, I have to admit I have done some pretty crusty things in the past to try and work it out, like checking for methods etc.. which always struck me as 1, quite possibly incorrect especially if I got a dictionary with an element with the same name as the method. Also I always thought it terribly inelegant. Should have looked in the source ;-) Thanks again T On Fri, 2002-07-12 at 04:10, Shane Hathaway wrote:
Tim Hoffman wrote:
Hi
I must be stupid or something, but I can't for the life of me work out a simple way of determining if a variable contains a string or array, in a PythonScript in Zope.
I can't import type and or use type() function. isinstance doesn't work because I can't give a type as the second arg.
Python scripts provide a special function, "same_type()", for this purpose. Example:
if same_type(s, ''): s = [s]
Shane
On Thu, Jul 11, 2002 at 05:22:48PM +0800, Tim Hoffman wrote:
I must be stupid or something, but I can't for the life of me work out a simple way of determining if a variable contains a string or array, in a PythonScript in Zope.
I can't import type and or use type() function. isinstance doesn't work because I can't give a type as the second arg.
I obviously just can't see the wood for the trees, can anyone help out this silly individual ?
Testing for string methods works :) if hasattr(item, 'startswith'): # A String else: # Something else On a similar note you can test for a specific list method. -- Martijn Pieters | Software Engineer mailto:mj@zope.com | Zope Corporation http://www.zope.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
participants (3)
-
Martijn Pieters -
Shane Hathaway -
Tim Hoffman