views:

347

answers:

3

So I'm brand new to SQLAlchemy, and I'm trying to use the SQL Expression API to create a SELECT statement that specifies the exact columns to return. I found both a class and a function defined in the sqlalchmey.sql.expressions module and I'm not too sure which to use... Why do they have both a class and a function? When would you use one over the other? And would anyone be willing to explain why they need to have both in their library? It doesn't really make much sense to me to be honest, other than just to confuse me. :) JK

Thanks for the help in advance!

A: 

I think it's pretty much the same. The documentation says for select (the function):

The returned object is an instance of Select.

As you can pass the select function the same parameters that Select.__init__() accepts, I don't really see a difference. At first glance the arguments of the class constructor seem to be a superset of the function's. But the function can be passed any of the constructor's keyword arguments.

jellybean
That's what I'm trying to figure out too... what's the point of having both? Any ideas why they did this?
John
+3  A: 

Use the source.

Here's the implementation of the select function, from the source code:

def select(columns=None, whereclause=None, from_obj=[], **kwargs):
    """Returns a ``SELECT`` clause element.
    (... long docstring ...)
    """
    return Select(columns, whereclause=whereclause, from_obj=from_obj, **kwargs)

So, it is exactly the same.

nosklo
Ahhh... so is it just a matter of convenience/preference? I don't really see the point of having both, because you would import both using the same path (except for the change in case for the letter 's', of course).
John
+1  A: 

the expression package provides Python functions to do everything. These functions in some cases return a class instance verbatim from the function's arguments and other times compose an object from several components. It was originally the idea that the functions would be doing a lot more composition than they ended up doing in the end. In any case, the package prefers to stick to pep-8 as far as classes being in CamelCase, functions being all lowercase, and wanted the front end API to be all lower case - so you have the public "constructor" functions.

The SQL expression language is very easy to grok if you start with the tutorial.

zzzeek