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
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,
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
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
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
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
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
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__')
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:
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
--
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,
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
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()) ==
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):
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
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
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,
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
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).
--
19 matches
Mail list logo