[Tutor] recursive problem

Roelof Wobben rwobben at hotmail.com
Fri Sep 10 13:14:45 CEST 2010


Hello , 

 

So my book teach me the wrong principle.

But can everything programmed on Eafp.

 

If you dont know if something is a list, a tuple or a string, you can get a lot of nested try except think.

 

Roelof


 


Subject: Re: [Tutor] recursive problem
From: rabidpoobear at gmail.com
Date: Thu, 9 Sep 2010 15:32:44 -0500
To: rwobben at hotmail.com


No you misunderstood me. Eafp is a python design principle. lbyl is common in older languages. Please reply all in the future so our discussion takes place on the list instead of just between us.

Sent from my iPhone

On Sep 9, 2010, at 1:31 PM, Roelof Wobben <rwobben at hotmail.com> wrote:




Oke, 
 
So If I understand you right LBYL is more the python way.
Wierd that the book im following (Thinking like a computer scientist) is more EAFP.
 
Roelof

 


Subject: Re: [Tutor] recursive problem
From: rabidpoobear at gmail.com
Date: Thu, 9 Sep 2010 13:24:06 -0500
To: rwobben at hotmail.com


It's easier to ask for forgiveness than permission vs. Look before you leap. An example of LBYL would be checking the type of a variable before you use it. EAFP would be just using the variable, and if something goes wrong, handle the exception. It's a core tenet of python software design and goes hand in hand w/ duck typing and other principles.

Sent from my iPhone

On Sep 9, 2010, at 1:16 PM, Roelof Wobben <rwobben at hotmail.com> wrote:





 Sorry. 
 
Im also new to Python and programming.
 
What does EAFP and LBYL mean ?
 
Roelof
 


From: rabidpoobear at gmail.com
Date: Thu, 9 Sep 2010 12:59:46 -0500
To: joel.goldstick at gmail.com
CC: tutor at python.org
Subject: Re: [Tutor] recursive problem


Nope Joel, that's what I meant. Remember EAFP over LBYL! I'm not sure the best way to make sure the object is iterable though.

Sent from my iPhone

On Sep 9, 2010, at 11:41 AM, Joel Goldstick <joel.goldstick at gmail.com> wrote:







On Thu, Sep 9, 2010 at 12:07 PM, Steven D'Aprano <steve at pearwood.info> wrote:




On Fri, 10 Sep 2010 01:05:22 am Joel Goldstick wrote:
> On Thu, Sep 9, 2010 at 10:26 AM, Luke Paireepinart
>
> <rabidpoobear at gmail.com>wrote:
> > Shouldn't there be a way to do this without type checking? Duck
> > typing!
> >
> > Your post got me thinking.  Maybe better to test if the object can
> > return
> an iter method.  If it throws an error, then look at its value.  If
> it doesn't, then its a list or a tuple


It's not clear what you mean by "return an iter method". Taken
literally, that would imply the object is a function. I think you
mean "*has* an iter method" -- except that's not right either:

>>> [].iter
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'iter'


Perhaps you mean an object which can be passed to iter(), but lots of
objects can do that, not just lists and tuples:

>>> iter("not a list or tuple")
<str_iterator object at 0xb7d3520c>
>>> iter({1: None, 2: "a", 4: 5})
<dict_keyiterator object at 0xb7d3420c>


I was googling, and found that if an object has an __iter__ method it will return it.  If not it will throw an error.  You are right about more than lists and tuples being iterable.  But, in this thread, it was brought up that checking type may not be pythonic.  If you wanted to use the same code to find values in a nested list of any objects, you could dispense with the type checking and just see if it is iterable.


I'm new to python.  Am I off base? 



--
Steven D'Aprano



_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


-- 
Joel Goldstick



_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor  		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100910/4da368c4/attachment.html>


More information about the Tutor mailing list