Re: Test for structure

2005-02-22 Thread Martin Miller
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 -- http://mail.python.org/m

Re: Test for structure

2005-02-22 Thread Martin Miller
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Test for structure

2005-02-22 Thread Martin Miller
At the end of his last post, Steve Bethard wrote: > That said, I find that in most cases, the better option is to use *args > in the original function though. For example: > > def f(arg): > args = aslist(arg) > ... > f(42) > f(['spam', 'eggs', 'ham']) > > could pro

Re: Test for structure

2005-02-21 Thread Terry Reedy
"Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 s

Re: Test for structure

2005-02-21 Thread Steven Bethard
Martin Miller broke the order of reading again by top-posting: However, to handle the more general problem of allow *any* argument to be either a single item or a list seems to require a combination of both EAPF and LBYL. This is the best solution I've been able to come up with so far: def asList(a

Re: Test for structure

2005-02-21 Thread Martin Miller
Testing for the '__iter__' (or even '__getitem__') attribute doesn't really address the problem, nor does trying to execute the statement 'itr = iter(a)'. To use EAPF and answer the OP's original question, which was > So how can I test if a variable 'a' is either a single character > string or a

Re: Test for structure

2005-02-21 Thread Steven Bethard
> Steven Bethard wrote: >> >>Right. str and unicode objects support iteration through the old >>__getitem__ protocol, not the __iter__ protocol. If you want to use >>something as an iterable, just use it and catch the exception: >> >>try: >> itr = iter(a) >>except TypeError: >> # 'a' is n

Re: Test for structure

2005-02-21 Thread Martin Miller
Yes, both string and lists have a __getitem__ attribute: >>> c1 = 'abc' >>> c2 = ['de', 'fgh', 'ijkl'] >>> hasattr(c1, '__getitem__') True >>> hasattr(c2, '__getitem__') True In other words you could index elements of either one using []. Likewise, both a string and list would produce a usable i

Re: Test for structure

2005-02-20 Thread Steven Bethard
Terry Hancock wrote: > But you probably shouldn't do that. You should probably just test to > see if the object is iterable --- does it have an __iter__ method? > > Which might look like this: > > if hasattr(a, '__iter__'): > print "'a' quacks like a duck" Martin Miller top-posted: I don't beli

Re: Test for structure

2005-02-20 Thread Martin Miller
I don't believe you can use the test for a __iter__ attribute in this case, for the following reason: >>> c1 = 'abc' >>> c2 = ['de', 'fgh', 'ijkl'] >>> hasattr(c1, '__iter__') False >>> hasattr(c2, '__iter__') True >>> for i in c1: print "i=%s is an element of c1" % repr(i) ... i='a' is an element

Re: Test for structure

2005-02-18 Thread Terry Hancock
On Wednesday 16 February 2005 09:08 am, alex wrote: > how can I check if a variable is a structure (i.e. a list)? For my > special problem the variable is either a character string OR a list of > character strings line ['word1', 'word2',...] > > So how can I test if a variable 'a' is either a sing

Re: Test for structure

2005-02-16 Thread Ben Finney
alex wrote On 17/02/05 02:08: how can I check if a variable is a structure (i.e. a list)? For my special problem the variable is either a character string OR a list of character strings line ['word1', 'word2',...] You're trying to apply the LBYL principle. My bet is that your "special problem" c

Re: Test for structure

2005-02-16 Thread Steven Bethard
Michael Hartl wrote: I use a function isListLike in cases such as this one: # def isListLike(L): # """Return True if L is list-like, False otherwise.""" # try: # L + [] # return True # except: # return False Then you can use a standard if-else construct: # if isL

Re: Test for structure

2005-02-16 Thread Michael Hartl
I use a function isListLike in cases such as this one: # def isListLike(L): # """Return True if L is list-like, False otherwise.""" # try: # L + [] # return True # except: # return False Then you can use a standard if-else construct: # if isListLike(myvar): #

Re: Test for structure

2005-02-16 Thread Steven Bethard
alex wrote: So how can I test if a variable 'a' is either a single character string or a list? py> def test(x): ... return (isinstance(x, list) or ... isinstance(x, basestring) and len(x) == 1) ... py> test('a') True py> test('ab') False py> test([]) True py> test(['a', 'b']) True B

Re: Test for structure

2005-02-16 Thread Diez B. Roggisch
import types v = [] if type(v) is types.ListType: pass -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list

Re: Test for structure

2005-02-16 Thread Chris Cioffi
Perhaps you're looking for the type() built in function and the types modules? >>> type('aaa') >>> type([]) >>> import types >>> if type([]) is types.ListType: ... print 'is a list' ... is a list Chris On Wed, 16 Feb 2005 07:10:56 -0800 (PST), alex <[EMAIL PROTECTED]> wrote: > Hi ther

Re: Test for structure

2005-02-16 Thread Simon Brunning
On Wed, 16 Feb 2005 07:11:08 -0800 (PST), alex <[EMAIL PROTECTED]> wrote: > how can I check if a variable is a structure (i.e. a list)? For my > special problem the variable is either a character string OR a list of > character strings line ['word1', 'word2',...] > > So how can I test if a variabl

Test for structure

2005-02-16 Thread alex
Hi there, how can I check if a variable is a structure (i.e. a list)? For my special problem the variable is either a character string OR a list of character strings line ['word1', 'word2',...] So how can I test if a variable 'a' is either a single character string or a list? I tried: if a is li