Thanks - you're right, I do see this often. I did something similar at 
first. I changed to performing it in the __init__ function since:
1) I can group similar init code as I develop it
2) I have multiple database and front-end sources that might create an 
Event object, so I might end up with awkward/long/not-unique function names 
such as from_db_users_profile_mini

If it's not hurting anything, I suppose I'll leave it like it is. My main 
concerns would be performance or making something in the future which I 
haven't done yet more difficult, such as fully-covered unit testing.

On Thursday, October 9, 2014 8:55:59 PM UTC-4, Chris Rossi wrote:
>
> 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.g...@gmail.com 
> <javascript:>> 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-discus...@googlegroups.com <javascript:>.
>> To post to this group, send email to pylons-...@googlegroups.com 
>> <javascript:>.
>> 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