473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 <sis not string
#then <return>, but how should write the <??
#Or is there any other way to do it?

Any suggestion would be appreciate

Peace

Sep 21 '06 #1
11 1734
def test(s):
if type(s) != ? :
return
#So here I want establish a situation about that if <sis not string
#then <return>, but how should write the <??
#Or is there any other way to do it?
>>isinstance("hello", basestring)
True
>>isinstance(u"hello", basestring)
True

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.

HTH,

-tkc


Sep 21 '06 #2
Thank you so much it answers my humble question perfectly:)

Sep 21 '06 #3
Tim Chase <py*********@tim.thechases.comwrote:
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 ...
Sep 21 '06 #4
Or yes that seems a handy way:)

Thanks for all wonderful people here:)

Peace
Duncan Booth wrote:
Tim Chase <py*********@tim.thechases.comwrote:
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 ...
Sep 21 '06 #5
In article <11*********************@k70g2000cwa.googlegroups. com>,
<br*********@gmail.comwrote:
>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.
Sep 21 '06 #6
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 <11*********************@k70g2000cwa.googlegroups. com>,
<br*********@gmail.comwrote:
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.
Sep 21 '06 #7
br*********@gmail.com 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 'o****@xiludom.gro'.split('@')])"
Sep 21 '06 #8
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:
br*********@gmail.com 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 'o****@xiludom.gro'.split('@')])"
Sep 21 '06 #9
br*********@gmail.com wrote:
(please, *stop* top-posting - corrected)
Bruno Desthuilliers wrote:
>>
br*********@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 'o****@xiludom.gro'.split('@')])"
Sep 21 '06 #10
In article <11**********************@b28g2000cwb.googlegroups .com>,
<br*********@gmail.comwrote:
Sep 21 '06 #11
I've learned a lot from you two, thank you:)

Peace

Sep 22 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2399
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
12
4103
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
1
2104
by: Daylor | last post by:
how can i return string to .Net application from c dll ? i have the text in char sText ; and i want to return it from c dll to .Net application. i tried : *char in the function, but the...
5
6115
by: krwill | last post by:
I'm trying to automate a combo box to add a record to the source table if it's "Not In List". I've tried many different examples and none have worked. Combo Box Name = Combo24 Source Table...
8
3275
by: John Nagle | last post by:
Here's a wierd problem: I have a little test case for M2Crypto, which just opens up SSL connections to web servers and reads their certificates. This works fine. But if I execute ...
13
3637
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
4
9041
by: jk2l | last post by:
Error 10 error LNK2019: unresolved external symbol __imp__glBindTexture@8 referenced in function "public: void __thiscall GLTexture::Use(void)" (?Use@GLTexture@@QAEXXZ) GLTexture.obj Error 11 error...
11
12882
by: askalottaqs | last post by:
i created a function which u send a string, and it tokenizes according to spaces, so i send it a string for it to return me a string array, but on the declaration of the function it says error...
4
1813
by: lostlander | last post by:
In ARMCC, and Microsoft C, when i use a function which is never defined or delared, it gives out a warning, not a compiling error? why? (This leads to a bug to my program since I seldom pay much...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.