A common pattern is class methods as constructors:

class MyThing(object):

    @classmethod
    def from_db(cls, row):
        obj = cls()
        ... # initialize attributes from database row
        return obj

    @classmethod
    def from_json(cls, data):
        obj = cls()
        ... # initialize attributes from json data
        return obj

    def __init__(self):
       pass



On Thu, Oct 9, 2014 at 5:41 PM, Taylor Gronka <mr.gro...@gmail.com> wrote:

> I have an Event class that creates an object when "events" are pulled from
> the database.  It also creates event objects from user input - the user can
> then commit() these events to the database.
>
> So, event objects are created from two different sources (actually more,
> but, for simplicity..): from 1) user input coming from javascript, and 2)
> from a database lookup.
>
> I want to run different initialization code depending on which source the
> data is coming from - such as, I want to error-check user input, but I
> don't need to error-check database lookups.  What are some python/pyramid
> best practices?  Is there a term for this?   I think inheritance would
> create too many subclasses and wonky coding.  "Overloading" doesn't seem
> like the right term for it.
>
> Right now, I am passing a {'source': 'none'/'html'/'database'/'etc'}
> parameter to the Event.__init__().  I then use if statements to go from
> there.  It works and it's easy to add a new 'source', but it seems sloppy.
>
> Thanks for any help.
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pylons-discuss+unsubscr...@googlegroups.com.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.
>

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

Reply via email to