Main Content
Embedding more complex code into a named block Archive - Originally posted on "The Horse's Mouth" - 2016-11-04 07:38:56 - Graham Ellis
If you're writing a complicated piece of code / awkward algorithm, it's a good idea to put it into a separate function. That way, it can be debugged and tested on its own, it can re-used in other code later on, and it can be hidden away from other users - and indeed from you (the author) unless you need to revisit it.
On our Python courses , one of the exercises we set is to ask delegates to take a list of month lengths:
mlen = [31,29,31,30,31,30,31,31,30,31,30,31]
and produce two lists - one with 31 x the number 1, then 29 x the number 2 and so on, so that they have an ability to look up (for example) that day 70 of the year is in month three. The second list to contain 1 to 31, the 1 to 29, and so on - so they can also quickly look up which day of the month that day 70 of the year is.
It turns out the the algorithm also works for other things - for example converting working days of the year into week and day number (taking public holidays into account for short weeks, for example!).
Full solution to the original problem - extended beyond what I ask delegates to do in oder to allow code reuse - [here] and an example of that re-use [here]
Some other articles
Y105 - Functions, Modules and Packages From and Import in Python - where is the module loaded from? This article Nesting decorators Recursion in Python - the classic example What are callbacks? Why use them? An example in Python What is the difference between a function and a method? Reading command line parameters in Python A good example of recursion - a real use in Python Python - even named code blocks are objects Multiple yields and no loops in a Python generator? Python functions - an introduction to how they work Python varables - checking existance, and call by name or by value? Exception, Lambda, Generator, Slice, Dict - examples in one Python program vargs in Python - how to call a method with unknown number of parameters Optional positional and named parameters in Python Default local - a good choice by the author of Python Static variables in Python? Python timing - when to use a list, and when to use a generator Functions are first class variables in Lua and Python Finding all the unique lines in a file, using Python or Perl Python Packages - groupings of modules. An introduction Static variables in functions - and better ways using objects Passing optional and named parameters to python methods Catching the fishes first? Passing parameters to Python functions - the options you have Returning multiple values from a function call in various languages - a comparison Using an exception to initialise a static variable in a Python function / method Python - some common questions answered in code examples Passing a variable number of parameters in to a function / method Program for reliability and efficiency - do not duplicate, but rather share and re-use Optional and named parameters to Python functions/methods Python - access to variables in the outer scope Global and Enable - two misused words! Good example of recursion in Python - analyse an RSS feed Sample code with errors in it on our web site Optional parameters to Python functions Multiple returns from a function in Python Conversion of OSI grid references to Eastings and Northings Dynamic code - Python Optional and named parameters in Python What to do with a huge crop of apples Anonymous functions (lambdas) and map in Python Sharing variables with functions, but keeping them local too - Python Global - Tcl, PHP, Python Python Script - easy examples of lots of basics Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Function / method parameters with * and ** in Python It's the 1st, not the 1nd 1rd or 1th. Sludge off the mountain, and Python and PHP Python - A list of methods Recursion in Python Python - function v method Dynamic functions and names - Python Do not duplicate your code Cottage industry or production line data handling methods Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. Python - block insets help with documentation Python's Generator functions Difference between import and from in Python What is a callback? Code and code maintainance efficiency Call by name v call by value Lambdas in Python Python generator functions, lambdas, and iterators Distance Learning Variable Scope Y104 - Lists and Tuples This article Shuffling a list - Ruby and Python Collections in Python - list tuple dict and string. Spike solutions and refactoring - a Python example Stepping through a list (or an array) in reverse order List slices in Python - 2 and 3 values forms, with an uplifting example All possible combinations from a list (Python) or array (Ruby) Beware - a=a+b and a+=b are different - Python Arrays of arrays - or 2D arrays. How to program tables. Copying - duplicating data, or just adding a name? Perl and Python compared Traffic lights in Python Python - fresh examples of all the fundamentals Strings as collections in Python Creating and iterating through Python lists Looking for a value in a list - Python Tektronix 4010 series / Python Tuples for loop - how it works (Perl, PHP, Java, C, etc) Python collections - mutable and imutable Python - extend v append on a list The ternary operator in Python Overloading of operators on standard objects in Python