Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Function parameter type safety?

Reply
Thread Tools

Function parameter type safety?

 
 
Robert Dailey
Guest
Posts: n/a
 
      07-12-2007
Hi,

Is there a way to force a specific parameter in a function to be a
specific type? For example, say the first parameter in a function of
mine is required to be a string. If the user passes in an integer, I
want to notify them that they should pass in a string, not an integer.

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      07-12-2007
Robert Dailey wrote:
> Hi,
>
> Is there a way to force a specific parameter in a function to be a
> specific type? For example, say the first parameter in a function of
> mine is required to be a string. If the user passes in an integer, I
> want to notify them that they should pass in a string, not an integer.
>


There is, but be careful. What about a string-like object, why would you want
to fail on that. It is better to attempt to do whatever you want to do with the
string and catch the exception.

Possible solution that I don't really like, but meets your explicit requirement:

def foo(arg1):
if isinstance(arg1, (basestring, unicode):

print "is string"
else:
print "is NOT string"

return


-Larry
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      07-12-2007
Robert Dailey wrote:
> Is there a way to force a specific parameter in a function to be a
> specific type? For example, say the first parameter in a function of
> mine is required to be a string. If the user passes in an integer, I
> want to notify them that they should pass in a string, not an integer.


In Python, you generally don't want to do this. If I have code like::

def foo(x):
if not isinstance(x, basestring):
raise ValueError('x must be a string')
return x.split()

then if someone creates a string-like object that doesn't subclass from
basestring, my code raises an exception even though the string-like
object had a .split() method and would have worked perfectly. If you
really feel like you need to give a specific error message, you could
write it instead like::

def foo(x):
try:
split = x.split
except AttributeError:
raise ValueError('x needs to have a split() method')
return split()

But generally there's no reason to do that because the AttributeError
itself would have said almost exactly the same thing.

Long story short: checking parameter types is un-Pythonic. Don't do it.

STeVe
 
Reply With Quote
 
Dave Baum
Guest
Posts: n/a
 
      07-12-2007
In article <. com>,
Robert Dailey <> wrote:

> Hi,
>
> Is there a way to force a specific parameter in a function to be a
> specific type? For example, say the first parameter in a function of
> mine is required to be a string. If the user passes in an integer, I
> want to notify them that they should pass in a string, not an integer.


At present, there isn't any built-in way to do this (see the recent
thread "PEP 3107 and stronger typing" for a long discussion).

However, you can use assert and isinstance() to check it manually:

def foo(a):
assert isinstance(a, str), "argument 'a' must be a string"


I wouldn't advocate getting carried away with this pattern since it
precludes your function from working with duck typing and defeats some
of the dynamic nature of Python. On the other hand, without such checks
the resulting exceptions from assuming an argument is one type when it
is another can be a bit misleading.

Dave
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-13-2007
Robert Dailey a écrit :
> Hi,
>
> Is there a way to force a specific parameter in a function to be a
> specific type?


No, and that's considered a GoodThing(tm).

> For example, say the first parameter in a function of
> mine is required to be a string. If the user passes in an integer, I
> want to notify them that they should pass in a string, not an integer.
>


Document the fact that the function expects a string as it's first
param. If someone pass an int, it's likely that he'll soon find out
there's something wrong (either from an exception or from obviously
incoherent results). This is usually enough.

If you're in the case that passing a non-string will not raise an
exception in the function but just lead to strange results or raise
somewhere else, you can eventually use an assertion to ease debugging -
but don't get into the habit of systematically type-check functions
arguments, this would be fighting against the language.

My 2 cents...
 
Reply With Quote
 
Robert Dailey
Guest
Posts: n/a
 
      07-13-2007
Good replies.

I'm in the process of learning Python. I'm a native C++ programmer, so
you can see how the question relates. There's a lot of cool things C++
allowed you to do with type-checking, such as function overloading.
With templates + type checking, I can create a STD version of ifstream/
ofstream in Python. However, I found it isn't possible since Python
mainly works with strings in file data instead of bytes. To write a
number 400000 to a file in python would take 6 bytes instead of 4, for
example. Anyway, I just wanted to explain why I was asking about type
checking. There's other measures I'm willing to take to get my job
done. Thanks guys.

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      07-13-2007
On Jul 13, 4:53 pm, Robert Dailey <rcdai...@gmail.com> wrote:
> Good replies.
>
> I'm in the process of learning Python. I'm a native C++ programmer, so
> you can see how the question relates. There's a lot of cool things C++
> allowed you to do with type-checking, such as function overloading.


This is an area of hot debate in Python and things are changing. You
may want
to have a look at http://www.python.org/dev/peps/pep-3119 about
interfaces,
and to http://www.python.org/dev/peps/pep-3124 about overloading and
generic functions. Both PEPs are for Python 3000, but their existence
should be an indication that people are not happy with the current
situation
in Python. You can something better than overloading already, with
P.J. Eby
modules simplegeneric and/or RuleDispatch, but I would say that they
are not
commonly used. So the right thing to do for now is to follow the good
advices you received, but keep in mind that there will be alternatives
in
the near future (such as interface checking/function overload).

Michele Simionato

 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      07-14-2007
On Jul 12, 5:52 pm, Robert Dailey <rcdai...@gmail.com> wrote:

> Hi,
>
> Is there a way to force a specific parameter in a function to be a
> specific type?


Yes; have a look at typecheck (http://oakwinter.com/code/typecheck/)
and FormEncode (http://formencode.org/Validator.html).

George

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      07-14-2007
En Fri, 13 Jul 2007 11:53:41 -0300, Robert Dailey <>
escribió:

> However, I found it isn't possible since Python
> mainly works with strings in file data instead of bytes. To write a
> number 400000 to a file in python would take 6 bytes instead of 4, for
> example.


py> import struct
py> f = open("output.bin","wb")
py> f.write(struct.pack("l", 400000))
py> f.close()
py> ^Z


C:\TEMP>dir output.bin
El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 3828-F1AF

Directorio de C:\TEMP

14/07/2007 00:13 4 output.bin
1 archivos 4 bytes
0 dirs 18.806.763.520 bytes libres

"strings" are "strings of bytes" in Python. (If you are mostly concerned
with characters, use unicode objects).

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter Type/Size Property Values for VARCHAR(MAX) Stored Proc Parameter jcpc ASP .Net 2 01-26-2011 11:48 AM
type declaration in declaration of a parameter or return type of a function Luca Forlizzi C Programming 4 11-14-2010 09:30 PM
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57