How to return an "not string' error in function?
first of all I have to claim that I'm a noob so please help me don't blame me:) for example: def test(s): if type(s) != ? : return #So here I want establish a situation about that if is not string #then , but how should write the ? #Or is there any other way to do it? Any suggestion would be appreciate Peace -- http://mail.python.org/mailman/listinfo/python-list
Re: How to return an "not string' error in function?
Thank you so much it answers my humble question perfectly:) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to return an "not string' error in function?
Or yes that seems a handy way:)
Thanks for all wonderful people here:)
Peace
Duncan Booth wrote:
> Tim Chase <[EMAIL PROTECTED]> wrote:
>
> > This will return true for both regular strings and for unicode
> > strings. If that's a problem, you can use
> >
> > >>> import types
> > >>> isinstance("hello", types.StringType)
> > True
> > >>> isinstance(u"hello", types.StringType)
> > False
> > >>> isinstance("hello", types.UnicodeType)
> > False
> > >>> isinstance(u"hello", types.UnicodeType)
> > True
> >
> > ...or, if you don't want to qualify them with "types." each time,
> > you can use
> >
> > >>> from types import StringType, UnicodeType
> >
> > to bring them into the local namespace.
>
> They already are in the builtin namespace under their more usual names of
> str and unicode respectively, so there is no need to import them, just use
> them:
>
> >>> isinstance("hello", str)
> True
>
> ... etc ...
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to return an "not string' error in function?
Thank you for your inputing which has been great inspirational:) What I tried to do is to write a string.split() module, so I started with: def spilt(a): l=[] index=0 if not isinstance(a, basestring): #Or isinstance(a, str) return for i in len(a): if a[i]=' ': item=a[index:i] l.append(item) .. I'm still working on it:) Thank you again Peace PS: Is str() the same as repr() ? Cameron Laird wrote: > In article <[EMAIL PROTECTED]>, > <[EMAIL PROTECTED]> wrote: > >Thank you so much it answers my humble question perfectly:) > > > > HOWEVER, to answer you final question, yes, there is a different > and, in general, better, way. While there's a lot to say about > good Python style and typing, I'll summarize at a high level: > you shouldn't have to check types. I can understand that you > are working to make a particular function particularly robust, > and are trying to account for a wide range of inputs. This is > healthy. In stylish Python, though, you generally don't need > type checking. How would it be, for example, if someone passed > the number 3 to your function. Is that an error? Do you want > it automatically interpreted as the string "3"? You can achieve > these results withOUT a sequence of > > if isinstance(... > elif isinstance(... > ... > > perhaps with something as simple as > > my_input = str(my_input). > > One of us will probably follow-up with a reference to a more > detailed write-up of the subject. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to return an "not string' error in function?
Thank you for your reminder:)
However I saw the split() function in the first place and that why I'm
trying write one myself:)
Peace
Bruno Desthuilliers wrote:
> [EMAIL PROTECTED] wrote:
> (OT : please dont top-post)
>
> > Thank you for your inputing which has been great inspirational:)
> >
> > What I tried to do is to write a string.split() module,
>
> So don't waste time:
>
> >>> "ab eced f aazaz".split()
> ['ab', 'eced', 'f', 'aazaz']
> >>> "ab-eced-ff-aazaz".split('-')
> ['ab', 'eced', 'ff', 'aazaz']
> >>>
>
>
>
> --
> bruno desthuilliers
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
> p in '[EMAIL PROTECTED]'.split('@')])"
--
http://mail.python.org/mailman/listinfo/python-list
