News for hackers. Stuff that matters.
Re:Python Best Practice Patterns by famousactress on Friday February 28, 2014 @12:31PM (#7320161)
For certain use-cases (like constructing queries for an ORM) or other things where you're effectively passing around curried ideas to eventually be executed, I think cascading methods is a huge win.
Re:Python Best Practice Patterns by jlujan on Friday February 28, 2014 @01:40PM (#7320619)
What you are referring to is a design pattern called fluent interfaces[1]. They do make for very usable APIs when used to represent pipelines and filters. They are also used heavily in creating domain specific language features. In your SQL example it works very well such as in SQLAlchemy. But in that example, the chained methods are building a query as opposed to mutating the actual data. Splitting hairs.

[1]http://en.m.wikipedia.org/wiki/Fluent_interface

Re:Python Best Practice Patterns by famousactress on Friday February 28, 2014 @02:37PM (#7320950)
> Splitting hairs.

Severely, since what you're doing is mutating the actual state-data of the query object.

Re:Python Best Practice Patterns by DasIch on Friday February 28, 2014 @03:01PM (#7321079)
In the case of SQLAlchemy you are not. The cascading methods on query objects create new query objects as it should be. Mutating objects with cascading methods is horrible API design as it suggests immutability where there is none.
Re:Python Best Practice Patterns by zzzeek on Friday February 28, 2014 @08:14PM (#7322625)
the API in some ways comes from that of Hibernate, which does actually mutate in place (see http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html...). I felt that keeping the existing object un-mutated is a lot more intuitive. I think the names that you choose for the methods do make a difference, e.g. Hibernate is saying "add()" which for whatever reason seems to imply "mutation" to me, not sure why.
Re:Python Best Practice Patterns by famousactress on Saturday March 01, 2014 @01:03PM (#7325304)
Having used hibernate extensively (and considering many of it's features remain completely unmatched in any other ORM I've encountered), I can say that I strongly dislike the idea of mutating the query in place. There's huge advantages to each chain returning an independent and valid query that can be executed, referenced, and added to. I don't see any upside to the Hibernate approach here, and it violates "Prefer Immutability".
Re:Python Best Practice Patterns by famousactress on Friday February 28, 2014 @03:37PM (#7321305)
Oh right, that's fair. Same in Django (and you're right, mutation in this case would be horrible design), I suppose I was mixing the metaphorical with the actual.
Re:Python Best Practice Patterns by Goopplesoft on Friday February 28, 2014 @12:51PM (#7320289)
Yeah these things aren't as black and white as both the blogger and the original commenter make it seem. Software design is very subjective.