Re: sufficiently pythonic code for testing type of function

2006-10-16 Thread Bruno Desthuilliers
Fredrik Lundh wrote: > Scott David Daniels wrote: > >> Nope. idempotent: f(f(x)) = f(x) >> That is, after doing it once, repeating it won't hurt. > > http://en.wikipedia.org/wiki/Idempotence_%28computer_science%29 > > > Thank you (Scott and Fredrik) for the correction. -- bruno desthuillier

Re: sufficiently pythonic code for testing type of function

2006-10-13 Thread Fredrik Lundh
Scott David Daniels wrote: > Nope. idempotent: f(f(x)) = f(x) > That is, after doing it once, repeating it won't hurt. http://en.wikipedia.org/wiki/Idempotence_%28computer_science%29 -- http://mail.python.org/mailman/listinfo/python-list

Re: sufficiently pythonic code for testing type of function

2006-10-13 Thread Scott David Daniels
Bruno Desthuilliers wrote: > ... idempotent -> no side effects. Nope. idempotent: f(f(x)) = f(x) That is, after doing it once, repeating it won't hurt. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list

Re: sufficiently pythonic code for testing type of function

2006-10-13 Thread Theerasak Photha
On 10/11/06, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Theerasak Photha wrote: > > On 10/11/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > > > >> can be a lot better than a 30-level traceback that ends with a line > >> looking something like > >> > >> fnut.index(gah) > > > > Despite lon

Re: sufficiently pythonic code for testing type of function

2006-10-12 Thread A.T.Hofkamp
On 2006-10-11, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > A.T.Hofkamp wrote: >> On 2006-10-11, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: >>> Now the real question : what if the object is not an instance of any of >>> the types, but still support the expected interface ? >>> >> >> one po

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread bruno de chez modulix en face
Theerasak Photha a écrit : > On 10/11/06, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > > > Now the real question : what if the object is not an instance of any of > > the types, but still support the expected interface ? > > Perhaps: > > try: > for attribute in ['foo', 'bar', '__baz__']: >

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread Bruno Desthuilliers
A.T.Hofkamp wrote: > On 2006-10-11, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: >> Now the real question : what if the object is not an instance of any of >> the types, but still support the expected interface ? >> > > one possible answer: Use ZopeInterfaces > (and ask objects 'do you implement

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread A.T.Hofkamp
On 2006-10-11, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > > Now the real question : what if the object is not an instance of any of > the types, but still support the expected interface ? > one possible answer: Use ZopeInterfaces (and ask objects 'do you implement interface X' rather than 'a

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread Bruno Desthuilliers
Theerasak Photha wrote: > On 10/11/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > >> can be a lot better than a 30-level traceback that ends with a line >> looking something like >> >> fnut.index(gah) > > Despite long experience with Perl, I am not a big follower of the > "goose_level: blah"

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread Fredrik Lundh
Theerasak Photha wrote: > I'll do that as soon as my green noggin figures out what 'idempotent' means. "Acting as if used only once, even if used multiple times", to quote the first explanation I saw on the google result page. and from the irony department, googling for "indempotent" provides an

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread Theerasak Photha
On 10/11/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > can be a lot better than a 30-level traceback that ends with a line > looking something like > > fnut.index(gah) Despite long experience with Perl, I am not a big follower of the "goose_level: blah" method of error reporting... > also,

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread Fredrik Lundh
Theerasak Photha wrote: >> Now the real question : what if the object is not an instance of any of >> the types, but still support the expected interface ? > > Perhaps: > > try: > for attribute in ['foo', 'bar', '__baz__']: > getattr(mystery_object, '__%s__' % attribute) > except Attribute

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread Theerasak Photha
On 10/11/06, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Now the real question : what if the object is not an instance of any of > the types, but still support the expected interface ? Perhaps: try: for attribute in ['foo', 'bar', '__baz__']: getattr(mystery_object, '__%s__' % attribu

Re: sufficiently pythonic code for testing type of function

2006-10-11 Thread Bruno Desthuilliers
Theerasak Photha wrote: > I wrote this for someone else to take an object and list of types, > then check if obj is one of those types, This is already what isinstance(obj, tuple_of_types) does. > raising an error otherwise. > > Is it enough to rely on side effects or absence thereof, or should

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread Theerasak Photha
On 10/11/06, Ben Finney <[EMAIL PROTECTED]> wrote: > "Theerasak Photha" <[EMAIL PROTECTED]> writes: > > > On 10/11/06, Ben Finney <[EMAIL PROTECTED]> wrote: > > > Why have you re-implemented (a less-functional version of) 'isinstance'? > > > > Ignorance, I think. But the issue is resolved now. Than

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread Theerasak Photha
On 10/11/06, Ben Finney <[EMAIL PROTECTED]> wrote: > "Theerasak Photha" <[EMAIL PROTECTED]> writes: > Far better is to take the objects passed, *use* them in the way that > you need to use them, and catch exceptions that get raised at the > point where there is enough context to handle them. This

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread Ben Finney
"Theerasak Photha" <[EMAIL PROTECTED]> writes: > On 10/11/06, Ben Finney <[EMAIL PROTECTED]> wrote: > > Why have you re-implemented (a less-functional version of) 'isinstance'? > > Ignorance, I think. But the issue is resolved now. Thank you. Hopefully it is also merely ignorance that leads you t

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread Ben Finney
"Theerasak Photha" <[EMAIL PROTECTED]> writes: > I wrote this for someone else to take an object and list of types, > then check if obj is one of those types, raising an error otherwise. This is a way of programming known as "Look Before You Leap" (LBYL). It is most likely a mistake to do this in

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread John Machin
Theerasak Photha wrote: > I wrote this for someone else to take an object and list of types, > then check if obj is one of those types, raising an error otherwise. > > Is it enough to rely on side effects or absence thereof, or should I > put return True in here somewhere? > > def test_obj_type(obj

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread Theerasak Photha
On 10/11/06, Ben Finney <[EMAIL PROTECTED]> wrote: > Why have you re-implemented (a less-functional version of) 'isinstance'? Ignorance, I think. But the issue is resolved now. Thank you. http://www.stanford.edu/~spqrsyc/crew/crew%20-%20ignorance.gif "It's amazing how much easier it is for a te

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread Ben Finney
"Theerasak Photha" <[EMAIL PROTECTED]> writes: > I wrote this for someone else to take an object and list of types, > then check if obj is one of those types, raising an error otherwise. > > Is it enough to rely on side effects or absence thereof, or should I > put return True in here somewhere? >

Re: sufficiently pythonic code for testing type of function

2006-10-10 Thread Fredrik Lundh
Theerasak Photha wrote: > I wrote this for someone else to take an object and list of types, > then check if obj is one of those types, raising an error otherwise. note that a call to your function is pretty much equivalent to assert isinstance(obj, types) which has the additional advan

sufficiently pythonic code for testing type of function

2006-10-10 Thread Theerasak Photha
I wrote this for someone else to take an object and list of types, then check if obj is one of those types, raising an error otherwise. Is it enough to rely on side effects or absence thereof, or should I put return True in here somewhere? def test_obj_type(obj, types): for type in types: i