[sqlalchemy] What is declarative_base() exactly?

2013-09-23 Thread Edward Kim
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.


Re: [sqlalchemy] What is declarative_base() exactly?

2013-09-23 Thread Claudio Freire
On Mon, Sep 23, 2013 at 10:22 AM, Edward Kim onward.ed...@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


-- 
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.


Re: [sqlalchemy] What is declarative_base() exactly?

2013-09-23 Thread Edward Kim
Oh, I see! It is return class literally. Thanks for your code.

On Monday, 23 September 2013 23:28:11 UTC+10, Klauss wrote:

 On Mon, Sep 23, 2013 at 10:22 AM, Edward Kim 
 onward...@gmail.comjavascript: 
 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 
  


-- 
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.


Re: [sqlalchemy] What is declarative_base() exactly?

2013-09-23 Thread Philip Scott
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.