Re: Specifing arguments type for a function

2006-07-04 Thread Magnus Lycka
Paolo Pantaleo wrote: I have a function def f(the_arg): ... and I want to state that the_arg must be only of a certain type (actually a list). Is there a way to do that? You can state that in your documentation. You're very likely to get a reasonable runtime error from this when you

Re: Specifing arguments type for a function

2006-06-23 Thread George Sakkis
Dennis Lee Bieber wrote: On 22 Jun 2006 16:48:47 -0700, George Sakkis [EMAIL PROTECTED] declaimed the following in comp.lang.python: What does __setitem__ have to do with iterability ? It confirms that the object is indexable, and mutable -- ie; a list, not a tuple or a string. Ok,

Re: Specifing arguments type for a function

2006-06-23 Thread George Sakkis
Dennis Lee Bieber wrote: On 22 Jun 2006 22:55:00 -0700, George Sakkis [EMAIL PROTECTED] declaimed the following in comp.lang.python: Ok, I'll try once more: What does __setitem__ have to do with **iterability**, not mutability or indexability ? I was commenting on Maric's post that

Re: Specifing arguments type for a function

2006-06-23 Thread Bruno Desthuilliers
George Sakkis wrote: Dennis Lee Bieber wrote: On 22 Jun 2006 16:48:47 -0700, George Sakkis [EMAIL PROTECTED] declaimed the following in comp.lang.python: What does __setitem__ have to do with iterability ? It confirms that the object is indexable, and mutable -- ie; a list, not a tuple

Re: Specifing arguments type for a function

2006-06-23 Thread Bruno Desthuilliers
George Sakkis wrote: Dennis Lee Bieber wrote: On 22 Jun 2006 22:55:00 -0700, George Sakkis [EMAIL PROTECTED] declaimed the following in comp.lang.python: Ok, I'll try once more: What does __setitem__ have to do with **iterability**, not mutability or indexability ? I was commenting on

Re: Specifing arguments type for a function

2006-06-22 Thread Bruno Desthuilliers
George Sakkis a écrit : Maric Michaud wrote: Le Mardi 20 Juin 2006 13:28, Maric Michaud a écrit : if not getattr(arg, '__iter__') and not getattr(arg, '__getitem__') : raise ValueError('Function accepts only iterables') # or error handling code oops, hasattr of course : if not

Re: Specifing arguments type for a function

2006-06-22 Thread bearophileHUGS
Paolo Pantaleo: and I want to state that the_arg must be only of a certain type (actually a list). Is there a way to do that? I suggest this very good library, typecheck: http://oakwinter.com/code/typecheck/ Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Specifing arguments type for a function

2006-06-22 Thread George Sakkis
Bruno Desthuilliers wrote: George Sakkis a écrit : This is ok - in theory. In practice I've found that e.g. strings are more often than not handled as scalars although they are typically iterables. hasattr('', '__iter__') False hasattr('', '__iter__') or hasattr('', '__getitem__')

Re: Specifing arguments type for a function

2006-06-21 Thread Mike Duffy
Paolo Pantaleo wrote: I have a function def f(the_arg): ... and I want to state that the_arg must be only of a certain type (actually a list). Is there a way to do that? I wrote a cool function decorator just for that purpose. It's posted on the Python Decorator Library at:

Specifing arguments type for a function

2006-06-20 Thread Paolo Pantaleo
I have a function def f(the_arg): ... and I want to state that the_arg must be only of a certain type (actually a list). Is there a way to do that? Thnx PAolo -- if you have a minute to spend please visit my photogrphy site: http://mypic.co.nr --

Re: Specifing arguments type for a function

2006-06-20 Thread Diez B. Roggisch
Paolo Pantaleo wrote: I have a function def f(the_arg): ... and I want to state that the_arg must be only of a certain type (actually a list). Is there a way to do that? Yes and no. You can ensure that the passed object is a list, by calling e.g. def f(arg): if not isinstance(arg,

Re: Specifing arguments type for a function

2006-06-20 Thread Rony Steelandt
Paolo Pantaleo wrote: I have a function def f(the_arg): ... and I want to state that the_arg must be only of a certain type (actually a list). Is there a way to do that? Yes and no. You can ensure that the passed object is a list, by calling e.g. def f(arg): if not

Re: Specifing arguments type for a function

2006-06-20 Thread Diez B. Roggisch
What about def f(arg): if type(arg)=='list': #do something Several things: - list is a string in your code - which wont work: type([]) == 'list' False It needs to be list w/o quotes. - you won't get sublclasses of list: class Foo(list): ...pass ... type(Foo()) ==

Re: Specifing arguments type for a function

2006-06-20 Thread bruno at modulix
Rony Steelandt wrote: Paolo Pantaleo wrote: I have a function def f(the_arg): ... and I want to state that the_arg must be only of a certain type (actually a list). Is there a way to do that? Yes and no. You can ensure that the passed object is a list, by calling e.g. def f(arg):

Re: Specifing arguments type for a function

2006-06-20 Thread Maric Michaud
Le Mardi 20 Juin 2006 12:29, Rony Steelandt a écrit : What about def f(arg):     if type(arg)=='list':         #do something And if arg's type is subclass of list ? The problem with isinstance is : and if arg is not of type list but is a sequence (a instance of UserList for example) ? The

Re: Specifing arguments type for a function

2006-06-20 Thread K.S.Sreeram
bruno at modulix wrote: if type(arg) is type([]): Just a tiny nitpick You can just use 'list' instead of 'type([])' if type(arg) is list : # blah blah Regards Sreeram signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list

Re: Specifing arguments type for a function

2006-06-20 Thread Maric Michaud
Le Mardi 20 Juin 2006 13:28, Maric Michaud a écrit : if not getattr(arg, '__iter__') and not getattr(arg, '__getitem__') :     raise ValueError('Function accepts only iterables') # or error handling code oops, hasattr of course : if not hasattr(arg, '__iter__') and not hasattr(arg,

Re: Specifing arguments type for a function

2006-06-20 Thread George Sakkis
Maric Michaud wrote: Le Mardi 20 Juin 2006 13:28, Maric Michaud a écrit : if not getattr(arg, '__iter__') and not getattr(arg, '__getitem__') : raise ValueError('Function accepts only iterables') # or error handling code oops, hasattr of course : if not hasattr(arg, '__iter__') and

Re: Specifing arguments type for a function

2006-06-20 Thread bruno at modulix
K.S.Sreeram wrote: bruno at modulix wrote: if type(arg) is type([]): Just a tiny nitpick You can just use 'list' instead of 'type([])' I know. Note that I wrote *A* right way to write this, not *The* right way... And FWIW, you could also use arg.__class__ instead of type(arg). --