[Tutor] Need help wrapping my head around duck typing

Izz ad-Din Ruhulessin izzaddin.ruhulessin at gmail.com
Wed May 25 01:57:58 CEST 2011


I forgot to mention
strptime<http://docs.python.org/library/datetime.html#strftime-strptime-behavior>

2011/5/25 Izz ad-Din Ruhulessin <izzaddin.ruhulessin at gmail.com>

> Hi Cory,
>
> From your description it seems to me that your case is more one of
> asserting that your function received valid arguments, than a matter of
> static-vs-ducktyping. Even with type checking, they can still generate
> errors (out of bounds, incorrect formatting).
>
> Now i've seen some discussions where people stated that type checking and
> argument assertion are bad and that the programmer should just let the code
> run until it bumps into and error. Python programmers are very inclined to
> "forgiveness is easier than permission", also referred to in the article you
> mentioned. However, the principle is only valid in cases where it is
> guaranteed that an incorrect type or value will produce and exception. For
> example, if you are interfacing between C and Python this can often not be
> the case -- or it will crash your programme without any kind of traceback
> (the dreaded segfault)
>
> Will an incorrect type or value raise an exception in your proposed
> solutions? It probably will, so it is probably best to go for
> try-except-finally clauses.
>
> A sidenote: personnally I would glob the directory and then sort the list
> by file creation date.
>
> Kind regards,
>
> Izz ad-Din Ruhulessin
>
> 2011/5/24 Cory Teshera-Sterne <ctsterne at gmail.com>
>
>> Hi folks,
>>
>> Coming from a language background that has a different approach to
>> variables (*cough* Java *cough*), I'm trying to understand Python's typing
>> conventions and how to deal with unknown variable types in my code. And as a
>> newbie, I'm really concerned with not writing code that will make the actual
>> Python programmer who will be maintaining my code jump out the window from
>> frustration.
>>
>> An example: I have a directory tree containing year directories (in the
>> form YYYY, ie, "2011"), which contain month directories (in the form MM, ie,
>> "05"), which in turn contain files. I'm writing a Python function to iterate
>> over the tree & return a list of the file names from either the full tree
>> (args==None) or a specified time period within that - ie, Feb. '10 to May
>> '11 (args==startdate, enddate). I'm not sure what the most pythonic approach
>> to dealing with the variables here would be. I have several options:
>>      - accept datetime objects and then get the year/month info from that
>>      - accept a string (and maybe check its length, add "20" if it's two
>> digits, or reject it if it's more than 4)
>>      - accept an integer (and maybe check that it's reasonable)
>>
>> My first inclination would be to only accept 4-digit year and 2-digit
>> month integers and fail the rest, but I understand the value of dealing with
>> different input types here. My second guess would be to use isinstance() for
>> each possibility, since different things need to happen in each case. The
>> "pythonic" approach, as best as I can gather, would be a try/catch block and
>> use the exception types to do the different processing. However, for
>> example, both trying to get a year from a string and a len() of an int raise
>> a TypeError, and in general I'm not sure how to approach this - or am I
>> overthinking things?
>>
>> Finally, I'm aware that I'm really bringing up two topics here:
>>
>> 1) What is the best approach in this and similar cases, in terms of
>> actual, working code that won't make the next user of this code cry? How
>> do/should I distinguish error types in this case?, and less importantly,
>>
>> 2) Can anyone help me get a good handle on the philosophical issues here?
>> I've read several discussions (both strongly against type-checking<http://www.canonical.org/%7Ekragen/isinstance/>and more
>> lenient<http://dobesland.wordpress.com/2007/10/07/python-isinstance-considered-useful/>,
>> as well as good general explanations<http://www.voidspace.org.uk/python/articles/duck_typing.shtml>),
>> but some of it's over my head, a lot of it says something like "I suggest we
>> change Python so it does X", and I'm not sure what it all means for me and
>> my little filename-grabbing script. It's all just a bunch of quaking to me
>> right now ...
>>
>> Thanks so much,
>> Cory
>>
>> _______________________________________________
>> 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/20110525/a720ddb1/attachment.html>


More information about the Tutor mailing list