How to return an "not string' error in function?

Bruno Desthuilliers onurb at xiludom.gro
Thu Sep 21 15:06:03 EDT 2006


breakfastea at gmail.com wrote:
(please, *stop* top-posting - corrected)

> Bruno Desthuilliers wrote:
>>
>> breakfastea at gmail.com wrote:
>> (OT : please dont top-post)
>>
>>> 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']
>>
> Thank you for your reminder:)
> 
> However I saw the split() function in the first place and that why I'm
> trying write one myself:)

Ok. Then if it's for learning, let's have a look :)

> def spilt(a):

def split(astring, sep=' '):

>     l=[]
>     index=0
>     if not isinstance(a, basestring): #Or isinstance(a, str)
>         return

It's an error, so you don't want to pass it silently:

      if not isinstance(astring, basetring):
          raise TypeError('expected a string or unicode, got : %s' \
                           % type(astring)

>     for i in len(a):

The common idiom is : "for item in sequence:". If you need indexes too,
use enumerate: "for i, char in enumerate(astring):".

But anyway, you may want to have a look at str.find() or str.index().

>         if a[i]=' ':
>             item=a[index:i]
>             l.append(item)


Good luck...

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list