
In Python
everything is an object - and that includes functions which are objects which contain blocks of code. And this means that you can define different functions of the same name depending on a condition, and you can replace a function with another one too, if you want to.
This really is valid:
ontop = int(raw_input("Enter 1 if summat ontop "))
if ontop == 1:
def tanksize(basedim):
vlume = basedim * (basedim+1) * (basedim+2)
return vlume
else:
def tanksize(basedim):
vlume = basedim * (basedim+2)
return vlume
for k in range(10):
more = tanksize(k)
print "more is ", more
def tanksize(basedim):
vlume = basedim * (basedim+4)
return vlume
for k in range(10):
more = tanksize(k)
print "more is now", more
Would you write code like that? Probably not - but you could well write a piece of code that embeds within another application as required, and overrides default (preloaded) code. A number of commercial applications use Python for their tailoring, and this is the mechanism they often use to provide hooks for extra code for the minority of users who want to change certain algorithms.
Illustration - delegates write a program using the topics I have just covered in my lecture on a programming course (this course on site in Germany) (written 2009-01-30, updated 2010-06-23)
Associated topics are indexed under
Y050 - Python - General [3935] Whether you have programmed before or not, we can teach you Python - (2012-11-25)
[3911] How well do you know Perl and / or Python? - (2012-11-04)
[3903] Python Programming class for delegates who have already self-taught the basics - (2012-10-25)
[3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
[3816] Want to escape the Olympics? Learn to program in the countryside! - (2012-07-23)
[3798] When you should use Object Orientation even in a short program - Python example - (2012-07-06)
[3519] Python - current versions and implementations (CPython, Jython, IronPython etc) - (2011-11-13)
[3489] Python courses and Private courses - gently updating our product to keep it ahead of the game - (2011-10-20)
[3463] Busy weekend of contrasts. - (2011-10-03)
[3076] Python through the Snow - (2010-12-01)
[2822] Python training courses for use with ESRI ArcMap software - (2010-06-23)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[2504] Learning to program in ... - (2009-11-15)
[2394] Two days of demonstration scripts in Python - (2009-09-05)
[2367] Learning to program - how to jump the first hurdles - (2009-08-20)
[2285] Great new diagrams for our notes ... Python releases - (2009-07-13)
[2227] Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11)
[2020] Learning Python - many new example programs - (2009-01-31)
[16] Python training - (2004-08-16)
Y199 - Python - Fast StartY106 - Object Oriented Python [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
[3878] From Structured to Object Oriented Programming. - (2012-10-02)
[3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
[3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[2169] When should I use OO techniques? - (2009-05-11)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
[1306] Python class rattling around - (2007-08-16)
[900] Python - function v method - (2006-10-20)
[834] Python makes University Challenge - (2006-08-15)
[477] Class, static and unbound variables - (2005-10-25)
5623
Some other Articles
Short health and safety courseBaby Caleb and Fortune City in your web logs?UnboundLocalError - Python MessagePython - a truly dynamic languageApache httpd and Apache Tomcat miscellanyService Excellence AwardsFirst ClasswxPython - Introduction and sampleThe Wiltshire Police