Checking Signature of Function Parameter

Travis Parks jehugaleahsa at gmail.com
Mon Aug 29 12:45:51 EDT 2011


On Aug 29, 2:30 am, Nobody <nob... at nowhere.com> wrote:
> On Sun, 28 Aug 2011 14:20:11 -0700, Travis Parks wrote:
> > More importantly, I want to make sure that
> > predicate is callable, accepting a thing, returning a bool.
>
> The "callable" part is do-able, the rest isn't.
>
> The predicate may accept an arbitrary set of arguments via the "*args"
> and/or "**kwargs" syntax, and pass these on to some other function.
> Exactly *which* function may be the result of an arbitrarily complex
> expression. Or it may not even call another function, but just use the
> arbitrary set of arguments in an arbitrarily complex manner.
>
> IOW, determining in advance what will or won't work is actually impossible.
>
>

Thanks for everyone's input. I decided that I will put some basic
checks up front, like "is it None", "is it Iterable" and "is it
callable". Other than that, I am letting things slide. Asking for
forgiveness is always easier anyway.

Just so everyone knows, I am defining these methods inside a class
called IterableExtender:

class IterableExtender(collections.Iterable):...

I wanted to allow for calls like this:

extend(range(0, 1000)).map(lambda x: x * x).where(lambda x: x % 2 ==
0).first(lambda x: x % 7 == 0)

It allows me to compose method calls similarly to LINQ in C#. I think
this looks better than:

first(where(map(range(0, 1000), lambda x: x * x, lambda x: x % 2 == 0,
lambda x : x % 7 == 0)))

Internally to the class, there are "private" static methods taking raw
inputs and performing no checks. The public instance methods are
responsible for checking input arguments and wrapping results.

Eventually, I will start working on algorithms that work on
MutableSequences, but for now I am just doing Iterables. This is
turning out to be a great learning experience.



More information about the Python-list mailing list