How to return an "not string' error in function?
breakfastea at gmail.com
breakfastea at gmail.com
Thu Sep 21 12:15:08 EDT 2006
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 <1158848968.493858.96600 at k70g2000cwa.googlegroups.com>,
> <breakfastea at gmail.com> 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.
More information about the Python-list
mailing list