On Thu, Apr 02, 2015 at 03:36:17PM -0400, WolfRage wrote: > On 04/02/2015 02:07 PM, Steven D'Aprano wrote: > <SNIP> > >>What are the best practices to create more Functional Python? > > > >Best practices: > > > >* Don't force your code to use one style exclusively. Use the most > > natural style for the task. Python makes it easy to mix functional, > > procedural, imperative and object oriented code in the one > > application. Use whatever is most natural for your task. > > Good point, but still trying to understand how this is best determined > beyond trial and error, and to make sure that my assumptions are correct > about this decision.
Practice and experience! > >* Where possible, write your functions and methods in a functional > > style. That means: > > > >- Avoid global variables. > > I have got this down. Just for the record, not all use of globals is bad. In my previous reply to Danny, I gave an example of "late binding" for a default parameter. In general, global constants, or near constants (e.g. you write to the variable once when your application starts up, perhaps after reading the value from a config file) are acceptable. > >- Avoid side-effects where possible. > > Now getting better at this. In object-oriented code, "side-effects" of course refers to side-effects outside of the object itself. An object is allowed to manipulate its own state. However, even in OO code, it is very useful to avoid side-effects. For example, the object might be immutable, so once it is initialised it cannot be changed. Even if your object is mutable, you should cleanly separate mutator methods which can modify the object from side-effect free methods which do not. E.g. it is okay for list.sort() to mutate the list, but list.index() should not! -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor