On Mon, Feb 10, 2014 at 7:13 AM, Bao Niu <niuba...@gmail.com> wrote: > I'm new to sqlalchemy. I tried to search this question but didn't come up > with accurate search terms. So I come here for some help from real people > instead of search engine. For the following code: >>> >>> class Places(Base): >>> >>> """""" >>> >>> __tablename__ = 'moz_places' >>> >>> >>> >>> id = Column(Integer, primary_key=True) >>> >>> url = Column(String) >>> >>> title = Column(String) >>> >>> rev_host = Column(String) >>> >>> visit_count = Column(Integer) >>> >>> hidden = Column(Integer) >>> >>> typed = Column(Integer) >>> >>> >>> >>> >>> #---------------------------------------------------------------------- >>> >>> def __init__(self, id, url, title, rev_host, visit_count, >>> >>> hidden, typed): >>> >>> """""" >>> >>> self.id = id >>> >>> self.url = url >>> >>> self.title = title >>> >>> self.rev_host = rev_host >>> >>> self.visit_count = visit_count >>> >>> self.hidden = hidden >>> >>> self.typed = typed > > > If I already defined each column names as class variable, why do I still > need to re-define each of them as instance variables? I just can't > understand the reason. Any hint would be highly appreciated. Thanks. >
The assigments inside your __init__ function aren't really "defining" instance variables. They are setting the values based on the parameters passed to the constructor. Python doesn't automatically initialise instance variables you - you need to do it yourself. *However*, when you are using the "declarative" extension to map your classes, you get a default __init__ method automatically, which will perform those assignments for you. So as long as you use this extension, you don't need to write trivial__init__ methods. See the note at http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#create-an-instance-of-the-mapped-class Hope that helps, Simon -- 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.