What is declarative_base() exactly?

597 views
Skip to first unread message

Edward Kim

unread,
Sep 23, 2013, 9:22:28 AM9/23/13
to sqlal...@googlegroups.com
Hi all,

I have a really short experience of python so it can be really stupid question.
I tried to understanding about declarative_base().

Example below:

Base = declarative_base()
class Bus(Base):
    __tablename__ = 'bus' 
    ....

In my understanding, That python code look like function or class. So it will be
return some value or instance.

In SQLAlchemy, declarative_base() return something and then, Bus class
inherit that Base. I saw the code in SQLAlchemy, But I can't understand what
exactly supposed to be.

>>> Base = declarative_base()
>>> Base
<class 'sqlalchemy.ext.declarative.api.Base'>

How this function is return class, not instance? Is it kind of design pattern?

I know It is not a big deal for just using SQLAlchemy, but I can't explain what it is
and how can return the class.

Please let me know what I need to know about this pattern or style.


Thanks,

Edward.

Claudio Freire

unread,
Sep 23, 2013, 9:28:11 AM9/23/13
to sqlal...@googlegroups.com
On Mon, Sep 23, 2013 at 10:22 AM, Edward Kim <onward...@gmail.com> wrote:
>> >>> Base = declarative_base()
>> >>> Base
>> <class 'sqlalchemy.ext.declarative.api.Base'>
>
>
> How this function is return class, not instance? Is it kind of design
> pattern?
>
> I know It is not a big deal for just using SQLAlchemy, but I can't explain
> what it is
> and how can return the class.


A class is an object like any other.

You can pass around references to classes like any other reference,
and you can create them just as well:

>>> def make_me_a_class():
... class A(object):
... def f(self):
... print "I'm a class"
... return A
...
>>> B = make_me_a_class()
>>> c = B()
>>> c.f()
I'm a class
>>>

Edward Kim

unread,
Sep 23, 2013, 9:35:27 AM9/23/13
to sqlal...@googlegroups.com
Oh, I see! It is return class literally. Thanks for your code.

Philip Scott

unread,
Sep 23, 2013, 9:36:41 AM9/23/13
to sqlal...@googlegroups.com
declarative_base is just a function that returns a class. In python, a class is a first class object just like any other. You can do things like this:

class MyClass(object):
    pass

def foo()
   return MyClass

my_class_instance = foo()()

In normal use of SQLAlchemy you don't need to think too hard about what actually goes on inside declarative_base; it's part of the magical alchemy that takes a class full of Column() objects and lets you build queries and look at data in instances of your mapped classes. For more information on that sort of design pattern, punch 'python metaclass' into your favourite search engine and allow your mind to be boggled.

- Phil


--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages