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


On Mon, Sep 23, 2013 at 2:22 PM, Edward Kim <onward.ed...@gmail.com> wrote:

> 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.
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to