Re: [sqlalchemy] What is the point to re-define each and every column in __init__ in a class?
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.
[sqlalchemy] What is the point to re-define each and every column in __init__ in a class?
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. -- 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 the point to re-define each and every column in __init__ in a class?
As far as I understand it, its just a helper to initialize a new object. Some things to keep in mind: - You don't /have/ to provide all parameters, if some of your columns have default values you can only provide the required set of arguments - You don't /have/ to do that but then when you want to setup a new model object you will have to initialize them one by one: places = Places() places.url = ... places.title = ... and so on and so forth. HTH AM On Sun, Feb 9, 2014 at 11:13 PM, 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. -- 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.