[Tutor] (2.7.3) Inexplicable change of type

Ray Jones crawlzone at gmail.com
Fri Sep 14 10:43:24 CEST 2012


The code:

def split_string(source,splitlist):
    idx = 0
   
    while idx < len(splitlist):
       
        if splitlist[idx] in source:
            source = ' '.join(source.split(splitlist[idx]))
           
        idx += 1
       
    source = source.split(' ')
    print "source is", source, "and its type is", type(source) # <--
    while "'' in
source:                                                            # <--
        source = source.remove('')

    return source

out = split_string('Hi! I am your Assistant Instructor, Peter.', '! ,.')
print out


The result:

source is ['Hi', '', 'I', 'am', 'your', 'Assistant', 'Instructor', '',
'Peter', ''] and its type is <type 'list'>
Traceback (most recent call last):
  File "/tmp/pytmp.py", line 18, in <module>
    out = split_string('Hi! I am your Assistant Instructor, Peter.', '! ,.')
  File "/tmp/pytmp.py", line 13, in split_string
    while '' in source:
TypeError: argument of type 'NoneType' is not iterable

Between the two arrows, 'source' inexplicably switches from <type list>
to <type NoneType>. Why?


Ray


More information about the Tutor mailing list